Skip to content

Instantly share code, notes, and snippets.

@ptaylor
Created March 5, 2017 11:04
Show Gist options
  • Save ptaylor/36464a2050a914eb69843e6b21b62831 to your computer and use it in GitHub Desktop.
Save ptaylor/36464a2050a914eb69843e6b21b62831 to your computer and use it in GitHub Desktop.
Python merge two dicts
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