Created
November 25, 2015 04:55
-
-
Save pansapiens/15d486e3d717bf1158ce to your computer and use it in GitHub Desktop.
Merge Python dictionaries
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
# This version accepts any number of dictionaries | |
# http://stackoverflow.com/a/26853961 | |
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. | |
""" | |
result = {} | |
for dictionary in dict_args: | |
result.update(dictionary) | |
return result | |
# This version only accepts two dictionaries | |
# def merge_dicts(a, b): | |
# merged = a.copy() | |
# merged.update(b) | |
# return merged |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment