Last active
March 20, 2019 14:07
-
-
Save jlcrow/6af11f8c1f3ca71a4ba8db645a432278 to your computer and use it in GitHub Desktop.
Merges two dictionaries recursively
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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