Skip to content

Instantly share code, notes, and snippets.

@gjcourt
Created April 8, 2013 23:42
Show Gist options
  • Save gjcourt/5341588 to your computer and use it in GitHub Desktop.
Save gjcourt/5341588 to your computer and use it in GitHub Desktop.
Find all differing keys in two dictionaries
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