Last active
September 9, 2017 00:29
-
-
Save Zetaphor/ea2495562efebde912ad66a3f491348b to your computer and use it in GitHub Desktop.
Merge Dictionaries
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
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