Content:
Dictionaries in Python are used to store data using key:value
pairs.
From time to time, you may wish to merge the contents of two dictionaries together. This article explains several techniques you can use to merge the contents of two dictionaries.
Using the | Operator
Python 3.9 introduced a new way to merge dictionaries.
Introduced as Python Enhancement Proposal 584 (PEP 584), the |
operator can be used to merge the contents of two dictionaries.
dict1 = {'a': 12, 'b': 7}
dict2 = {'c': 4, 'd': 18}
merged = dict1 | dict2
As dictionary keys are unique, this technique preserves the last-seen value for a given key. This value will come from the operand on the right-hand side of the operator.
dict1 = {'a': 12, 'b': 7}
dict2 = {'b': 15, 'c': 4, 'd': 18}
merged = dict1 | dict2 # {'a': 12, 'b': 15, 'c': 4, 'd': 18}
In this example, the value for key b
is taken from dict2
, as this dictionary is the right operand.
If you’re using Python 3.9 or above, this is the simplest and easiest way of merging two dictionaires.
Using Dictionary Unpacking
A second dictionary-merging technique is available for Python version 3.5 and above.
This technique uses dictionary unpacking, to unpack two dictionaries into a new dictionary.
It is denoted by a **
added before the variable name.
**dict
dict1 = {'a': 12, 'b': 7}
dict2 = {'c': 4, 'd': 18}
merged = {**dict1, **dict2} # {'a': 12, 'b': 7, 'c': 4, 'd': 18}
As before, the last-seen value for each key is retained, for keys that appear in multiple operands.
Using dictionary.update()
A third technique involves the dictionary.update()
method.
update()
takes either a dictionary, or an iterable
containing key:value
pairs as an input. The contents of the input are added to the original dictionary.
Despite the name, update()
both updates the values of existing keys, and adds items that don’t already exist in the original dictionary.
dict1 = {'a': 12, 'b': 7}
dict2 = {'c': 4, 'd': 18}
dict1.update(dict2) # {'a': 12, 'b': 7, 'c': 4, 'd': 18}
This method, as with the previous techniques, retains the last-seen value for each key.
However, unlike the other examples, the resulting merge is retained in one of the input dictionaries (in this case, dict1
.
If you want a distinct variable containing the merged dictionary, create a copy of one of the two inputs.
dict1 = {'a': 12, 'b': 7}
dict2 = {'c': 4, 'd': 18}
merged = dict1
merged.update(dict2) # {'a': 12, 'b': 7, 'c': 4, 'd': 18}
Using a Loop
All of the methods so far involve adding items from a second dictionary to the items from the first. For items existing in both dictionaries, the item from the second dictionary is retained.
For more complex merges, it might be desirable to choose a specific action to take where an item exists in both dictionaries.
The best way to handle this is to loop through the keys of the second dictionary. You can then run the required check on the value, before adding it to the first dictionary.
dict1 = {'a': 12, 'b': 7}
dict2 = {'c': 4, 'd': 18}
for key in dict2:
if key not in dict1 or dict2[key] > dict1[key]:
dict1[key] = dict2[key]
In this instance, the merged array will contain the greatest value for each key across the two input dictionaries.
Like the previous method, this method adds values to the first input dictionary. To store the output in a new variable, create a copy of the first input dictionary.
dict1 = {'a': 12, 'b': 7}
dict2 = {'c': 4, 'd': 18}
merged = dict1
for key in dict2:
if key not in merged or dict2[key] > merged[key]:
merged[key] = dict2[key]