Skip to content

Instantly share code, notes, and snippets.

@jlcrow
Last active March 20, 2019 14:07
Show Gist options
  • Save jlcrow/6af11f8c1f3ca71a4ba8db645a432278 to your computer and use it in GitHub Desktop.
Save jlcrow/6af11f8c1f3ca71a4ba8db645a432278 to your computer and use it in GitHub Desktop.
Merges two dictionaries recursively
import collections
def merge_dict(d1, d2):
for k,v2 in d2.items():
v1 = d1.get(k) # returns None if v1 has no value for this key
if ( isinstance(v1, collections.Mapping) and
isinstance(v2, collections.Mapping) ):
merge_dict(v1, v2)
else:
d1[k] = v2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment