Skip to content

Instantly share code, notes, and snippets.

@aarshtalati
Created June 8, 2016 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aarshtalati/d4d54bfb70b69453ac81cefcc11d874b to your computer and use it in GitHub Desktop.
Save aarshtalati/d4d54bfb70b69453ac81cefcc11d874b to your computer and use it in GitHub Desktop.
Python Compare two dictionaries
def compareDicts(d1, d2):
differences = []
# present in d1 but not in d2
for key, value in ({k: d1[k] for k in set(d1) - set(d2)}).iteritems():
temp = list[[key, value], None]
differences.append(temp)
# present in d2 but not in d1
diff2 = {k: d2[k] for k in set(d2) - set(d1)}
for key, value in (diff2).iteritems():
temp = [None, [key, value]]
differences.append(temp)
# updated from d1 to d2
updatedKeys = [item for item in list(set(d1.keys()) & set(d2.keys())) if d1.get(item) != d2.get(item)]
for key in updatedKeys:
temp = [[key, d1.get(key)],[key, d2.get(key)]]
differences.append(temp)
return differences
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment