Skip to content

Instantly share code, notes, and snippets.

@pansapiens
Created November 25, 2015 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pansapiens/15d486e3d717bf1158ce to your computer and use it in GitHub Desktop.
Save pansapiens/15d486e3d717bf1158ce to your computer and use it in GitHub Desktop.
Merge Python dictionaries
# 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