Skip to content

Instantly share code, notes, and snippets.

@Zetaphor
Last active September 9, 2017 00:29
Show Gist options
  • Save Zetaphor/ea2495562efebde912ad66a3f491348b to your computer and use it in GitHub Desktop.
Save Zetaphor/ea2495562efebde912ad66a3f491348b to your computer and use it in GitHub Desktop.
Merge Dictionaries
def merge_dicts(*dict_args):
"""
Given any number of dicts, shallow copy and merge into a new dict,
precedence goes to key value pairs in latter dicts.
This function will work in Python 2 and 3 for all dicts.
e.g. given dicts a to g: z = merge_dicts(a, b, c, d, e, f, g)
Key value pairs in g will take precedence over dicts a to f, and so on.
https://stackoverflow.com/questions/38987/how-to-merge-two-dictionaries-in-a-single-expression
"""
result = {}
for dictionary in dict_args:
result.update(dictionary)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment