Merge two dictionaries in Python
This post will discuss how to merge two dictionaries in Python.
1. Using update() function
The ideal solution to merge two dictionaries in Python is using the update() function. This function takes another dictionary as an argument and in-place updates the parent dictionary with the key-value pairs from the another dictionary. Here is a simple example demonstrating the usage of this function.
|
1 2 3 4 5 |
first = {'A': 1, 'B': 2, 'C': 3} second = {'D': 4, 'E': 5} first.update(second) print(first) # {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5} |
Note that if a key already exists in the parent dictionary, its value will be overwritten by the value from the second dictionary. If a key does not exist in the parent dictionary, it will be added along with its value from the other dictionary. For example:
|
1 2 3 4 5 |
first = {'A': 1, 'B': 2, 'C': 3} second = {'C': 4, 'D': 5} first.update(second) print(first) # {'A': 1, 'B': 2, 'C': 4, 'D': 5} |
2. Using Dictionary unpacking operator
Another Pythonic option to merge two dictionaries in Python is to use the dictionary unpacking operator (**) with dictionary comprehension. The ** operator was proposed in PEP 448 and requires Python 3.5 or higher. It can be used to unpack a dictionary into keyword arguments for a function call or a new dictionary literal. Here is an example of how to use this operator on two dictionaries, and enclosing them in curly braces to create a new dictionary containing key-value pairs from both dictionaries.
|
1 2 3 4 5 |
first = {'A': 1, 'B': 2, 'C': 3} second = {'D': 4, 'E': 5} d = { **first, **second } print(d) # {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5} |
Note that this method returns a new dictionary, without modifying the original dictionaries. If a key appears in both dictionaries, the value from the second dictionary will overwrite the value from the first dictionary. We can also use a dictionary constructor instead of dictionary comprehension, which requires unpacking of only the second dictionary.
|
1 2 3 4 5 |
first = {'A': 1, 'B': 2, 'C': 3} second = {'D': 4, 'E': 5} d = dict(first, **second) print(d) # {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5} |
3. Using itertools.chain() function
Another approach is to use the itertools.chain function, which is often used to treat consecutive sequences as a single sequence. We can get a new dictionary by passing the returned iterator to the dictionary constructor.
|
1 2 3 4 5 6 7 |
from itertools import chain first = {'A': 1, 'B': 2, 'C': 3} second = {'D': 4, 'E': 5} d = dict(chain(first.items(), second.items())) print(d) # {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5} |
If we need to combine the list of dictionaries into a single dictionary, we can use the chain.from_iterable function.
|
1 2 3 4 5 6 7 8 9 10 |
from itertools import chain dicts = [ {'A': 1, 'B': 2}, {'C': 3, 'D': 4}, {'E': 5, 'F': 6} ] d = dict(chain.from_iterable(map(dict.items, dicts))) print(d) # {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6} |
4. Using | operator
A fourth way to merge two dictionaries in Python is using the | operator. It was introduced in Python 3.9 as part of PEP 5841, and can be used to perform a union operation on two dictionaries. This creates a new dictionary that contains all the key-value pairs from both dictionaries. If a key appears in both dictionaries, the value from the second dictionary will overwrite the value from the first dictionary. Here is an example of how to use this operator:
|
1 2 3 4 5 |
first = {'A': 1, 'B': 2, 'C': 3} second = {'D': 4, 'E': 5} d = first | second print(d) # {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5} |
5. Using collections.ChainMap class
A fifth way to merge two dictionaries in Python is to use the collections.ChainMap class from the collections module. This class allows us to create a single view of multiple dictionaries, and any updates made to the ChainMap will be reflected in the underlying dictionaries. To get a new dictionary instead of a view of the existing dictionaries, we can pass the ChainMap to the dictionary constructor. For example:
|
1 2 3 4 5 6 7 |
import collections first = {'A': 1, 'B': 2, 'C': 3} second = {'D': 4, 'E': 5} d = dict(collections.ChainMap(first, second)) print(d) # {'D': 4, 'E': 5, 'A': 1, 'B': 2, 'C': 3} |
6. Converting to List
Finally, we can convert both dictionary items into a list of pairs and then pass the list to the dictionary constructor to get a new dictionary. Here is an example of this approach:
|
1 2 3 4 5 |
first = {'A': 1, 'B': 2, 'C': 3} second = {'D': 4, 'E': 5} d = dict(list(first.items()) + list(second.items())) print(d) # {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5} |
That’s all about merging two dictionaries in Python.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)