Skip to content

Instantly share code, notes, and snippets.

@porterjamesj
Created November 14, 2013 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save porterjamesj/7462871 to your computer and use it in GitHub Desktop.
Save porterjamesj/7462871 to your computer and use it in GitHub Desktop.
cute python function to tell if one dict is a subset of another that took me way too long to write
def dict_subset(d1, d2):
"""Return true if d2 is contained within d1."""
if type(d1) is not dict or type(d2) is not dict:
return False
res = []
for key, val in d2.items():
if key in d1.keys():
if type(val) is dict:
res.append(dict_subset(d1[key], val))
elif val != d1[key]:
return False
else:
return False
return all(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment