Merging and Combining Dictionaries in Python
Dictionaries are fundamental data structures in Python, used to store data in key-value pairs. A common task is to combine or merge two or more dictionaries into a single dictionary. This tutorial explores various techniques for accomplishing this, considering scenarios where you might want to modify an existing dictionary in place or create a new one without altering the originals.
Understanding the Goal
The objective is to take two dictionaries, for example:
orig = {
'A': 1,
'B': 2,
'C': 3,
}
extra = {
'D': 4,
'E': 5,
}
and produce a combined dictionary that contains all the key-value pairs from both. The desired output would be:
{'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5}
It’s crucial to consider how to handle potential key collisions—situations where both dictionaries have the same key. In such cases, the value from the latter dictionary generally takes precedence.
Method 1: The update()
Method
The simplest and most common approach is to use the update()
method. This method modifies the dictionary it’s called on by adding the key-value pairs from another dictionary.
orig = {
'A': 1,
'B': 2,
'C': 3,
}
extra = {
'D': 4,
'E': 5,
}
orig.update(extra) # Modifies 'orig' in place
print(orig) # Output: {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5}
If you don’t want to modify the original dictionary, create a copy first:
orig = {
'A': 1,
'B': 2,
'C': 3,
}
extra = {
'D': 4,
'E': 5,
}
dest = orig.copy() # Create a copy of 'orig'
dest.update(extra) # Update the copy
print(dest) # Output: {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5}
print(orig) # Output: {'A': 1, 'B': 2, 'C': 3} (original is unchanged)
Method 2: Dictionary Unpacking (Python 3.5+)
Python 3.5 introduced dictionary unpacking using the **
operator, providing a concise way to merge dictionaries:
orig = {
'A': 1,
'B': 2,
'C': 3,
}
extra = {
'D': 4,
'E': 5,
}
dest = {**orig, **extra} # Create a new dictionary
print(dest) # Output: {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5}
This method creates a new dictionary without modifying the originals. Later values for shared keys will overwrite earlier ones.
Method 3: Using ChainMap
(from collections
)
The ChainMap
from the collections
module offers a different approach. It doesn’t create a new dictionary but instead provides a view that chains together multiple dictionaries. When a key is accessed, ChainMap
searches the dictionaries in the order they were provided until the key is found.
from collections import ChainMap
orig = {
'A': 1,
'B': 2,
'C': 3,
}
extra = {
'D': 4,
'E': 5,
}
dest = ChainMap(orig, extra) # Create a ChainMap view
print(dest['A']) # Output: 1
print(dest['D']) # Output: 4
ChainMap
is useful when you want to access data from multiple dictionaries without creating a new one, but modifications to the ChainMap
will affect the first dictionary in the chain.
Method 4: Combining Items with dict()
You can also merge dictionaries by combining their items (key-value pairs) into a new dictionary using the dict()
constructor.
orig = {
'A': 1,
'B': 2,
'C': 3,
}
extra = {
'D': 4,
'E': 5,
}
dest = dict(list(orig.items()) + list(extra.items()))
print(dest)
This method is generally less efficient than update()
or dictionary unpacking, especially for large dictionaries, as it involves creating lists of items.
Handling Key Collisions
In all these methods, if both dictionaries have the same key, the value from the latter dictionary overwrites the value from the former. For example:
orig = {'A': 1, 'B': 2}
extra = {'A': 3, 'C': 3}
dest = {**orig, **extra} # Or use update() or any other method
print(dest) # Output: {'A': 3, 'B': 2, 'C': 3}
The value associated with the key ‘A’ in the dest
dictionary is 3, taken from the extra
dictionary.
Choosing the Right Method
- For simple merging and modifying an existing dictionary,
update()
is the most straightforward and efficient approach. - If you need to create a new dictionary without modifying the originals, dictionary unpacking (
**
) is concise and efficient. ChainMap
is useful when you want to view multiple dictionaries as one without creating a new one, but be aware that modifications affect the first dictionary in the chain.- Combining items with
dict()
is less efficient and generally should be avoided for large dictionaries.