Created
November 14, 2013 07:33
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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