Created
October 24, 2018 15:40
-
-
Save Avantol13/f51d852ab4f9e2954ff6cabed0753be9 to your computer and use it in GitHub Desktop.
Check if two dictionaries in Python have the same key structure/schema
This file contains hidden or 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 dictionaries_match(d1, d2): | |
# both are dicts, need to check values | |
if isinstance(d1, dict) and isinstance(d2, dict): | |
d1_keys = d1.keys() | |
d1_keys.sort() | |
d2_keys = d2.keys() | |
d2_keys.sort() | |
valid = d1_keys == d2_keys and all( | |
dictionaries_match(d1[k], d2[k]) for k in d1.keys() | |
) | |
return valid | |
# one is a dict, other isn't | |
elif isinstance(d1, dict) or isinstance(d2, dict): | |
return False | |
# neither are dicts, assume match | |
else: | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment