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.

Download  Run Code

 
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:

Download  Run Code

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.

Download  Run Code

 
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.

Download  Run Code

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.

Download  Run Code

 
If we need to combine the list of dictionaries into a single dictionary, we can use the chain.from_iterable function.

Download  Run Code

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:

Download  Run Code

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:

Download  Run Code

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:

Download  Run Code

That’s all about merging two dictionaries in Python.