Skip to content

Instantly share code, notes, and snippets.

@nicksnell
Created May 29, 2019 15:48
Show Gist options
  • Save nicksnell/284a4a6ba8ff64e778d7ff62746e6344 to your computer and use it in GitHub Desktop.
Save nicksnell/284a4a6ba8ff64e778d7ff62746e6344 to your computer and use it in GitHub Desktop.
Recursively work out deltas of numbers in dictionaries
def delta_dict(a, b):
result = {}
for key in a.keys():
if isinstance(a[key], dict):
result[key] = delta_dict(a[key], b.get(key, {}))
else:
result[key] = a[key] - b.get(key, 0)
return result
if __name__ == '__main__':
print(delta_dict({'a': {'c': 4, 'd': 2}, 'b': 2}, {'a': {'c': 8}, 'b': 4}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment