I have a program that compares two python dicts, and it works perfectly fine for non nested dictionaries with a depth of 1. The algorithm is pretty simple:
for key in dict1, if not in dict2
print "-" key
for key in dict2, if not in dict1
print "+" key
for dict2Value, if not equal to dict1Value
print "+" dict2value
print "-" dict1value
As I said it works fine with dicts that have a depth of 1, and are not nested. How should I change the algorithm to work with nested and dicts with greater depth, I have been stuck for a while.
my code:
def print_diff(dict1, dict2):
for n in dict1:
if n not in dict2:
print('- "' + str(n) + '":')
for n in dict2:
if n not in dict1:
print('+ "' + str(n) + '":')
continue
if dict2[n] != dict1[n]:
if type(dict2[n]) not in (dict, list):
print('- "' + str(n) + '" : "' + str(dict1[n]))
print('+ "' + str(n) + '" : "' + str(dict2[n]))
else:
if type(dict2[n]) == dict:
print_diff(dict1[n], dict2[n])
continue
return