Created
April 8, 2013 23:42
-
-
Save gjcourt/5341588 to your computer and use it in GitHub Desktop.
Find all differing keys in two dictionaries
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 diff_dict(d1, d2): | |
""" | |
Determine which keys have changed between two dictionaries | |
""" | |
def diff(d1, d2, r): | |
for k in d1: | |
if k not in d2: | |
r[k] = d1[k] | |
elif d1[k] != d2[k]: | |
if issubclass(d2[k].__class__, dict): | |
if k not in r: | |
r[k] = {} | |
diff(d1[k], d2[k], r[k]) | |
else: | |
r[k] = d1[k] | |
return r | |
r1 = {} | |
r2 = {} | |
diff(d1, d2, r1) | |
diff(d2, d1, r2) | |
return r1, r2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment