Created
May 5, 2022 14:14
-
-
Save wallacejme/487aee49d8df9213f4f058a233e2add6 to your computer and use it in GitHub Desktop.
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
# compare two dictionaries and return which items have different values | |
def dict_diff(dict1, dict2): | |
diff = {} | |
keys_d1 = dict1.keys() | |
keys_d2 = dict2.keys() | |
for key_d1 in keys_d1: | |
if key_d1 in keys_d2: | |
if dict1[key_d1] != dict2[key_d1]: | |
diff[key_d1] = (dict1[key_d1], dict2[key_d1]) | |
else: | |
diff[key_d1] = (dict1[key_d1], None) | |
only_in_d2 = list(set(keys_d2) - set(keys_d1)) | |
for d2_ex in only_in_d2: | |
diff[d2_ex] = (None, dict2[d2_ex]) | |
return dict(sorted(diff.items(), key=lambda item: item[0])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment