I need to compare only the keys of two nested dictionaries. The primary usage is for the live tests of external API responses to prevent response change.
For example, this two dictionary matched, however their values differ:
EDIT: this is a sample and the actual dictionaries have dynamic keys, probably larger, and consists of integers, strings, and boolean
dict1 = {"guid": {"id": {"addr": "foo", "creation_num": "4"}}}
dict2 = {"guid": {"id": {"addr": "bar", "creation_num": "2"}}}
I try to do this by resetting the values of dictionaries with this method:
def reset_values(dictionary, reset_value=0):
for key, value in dictionary.items():
if type(value) is dict:
dictionary[key] = reset_values(dictionary[key], reset_value)
else:
dictionary[key] = reset_value
return dictionary
This method works, but is there a more Pythonic and straightforward way?