Created
March 5, 2017 11:04
-
-
Save ptaylor/36464a2050a914eb69843e6b21b62831 to your computer and use it in GitHub Desktop.
Python merge two dicts
This file contains 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
def merge(a, b): | |
c = {} | |
for k in a: | |
c[k] = a[k] | |
for k in b: | |
if k in c: | |
v1 = c[k] | |
v2 = b[k] | |
if isinstance(v1, dict) and isinstance(v2, dict): | |
c[k] = merge(v1, v2) | |
else: | |
c[k] = v2 | |
else: | |
c[k] = b[k] | |
return c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment