Python: Compare 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

view raw
compareDicts.py
hosted with ❤ by GitHub

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s