How do I merge two dictionaries in a single expression in Python?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In Python, dictionaries hold data in key-value pairs, making them one of the most versatile data structures available. Merging two dictionaries is a common task in Python, and it can often be done succinctly in a single expression. This article delves into the methods available for merging dictionaries, explaining the mechanics behind each and offering practical examples.
Dictionary Merging in Python
Method 1: Using the update() Method
The update() method allows you to merge two dictionaries by adding the key-value pairs from one dictionary into another.
Explanation: In this method, dict1 is updated with dict2. If dict2 has keys that are already in dict1, their values will overwrite those in dict1.
Method 2: Dictionary Unpacking (Python 3.5+)
With the advent of Python 3.5, the ** operator allows for a more pythonic way to merge dictionaries via unpacking.
Explanation: Here, the ** operator unpacks key-value pairs from dict1 and dict2 into a new dictionary. If there are overlapping keys, values from dict2 prevail.
Method 3: The | Operand (Python 3.9+)
Starting with Python 3.9, the | operand provides a clean and efficient way to merge dictionaries.
Explanation: The use of the | operand produces a new dictionary combining both dictionaries, with dict2 values overwriting dict1's in the event of key conflicts.
Method 4: Using collections.ChainMap
collections.ChainMap lets you combine multiple dictionaries while retaining the dictionaries as separate entities.
Explanation: ChainMap creates a single view over a sequence of dictionaries. When retrieving a key, it returns the first occurrence found in the maps.
Considerations for Merging
- Key Conflicts:
update(),|, and dictionary unpacking all prioritize the last dictionary's value if keys are duplicated. - Memory Efficiency:
ChainMapdoes not physically combine dictionaries but keeps a logical combination, which can be more memory-efficient. - Immutability: Using
update()alters the dictionary in place; the other methods create a new dictionary.
Summary Table
| Method | Version | In-Place? | Key Conflict Resolution | Immutability | |
update() | - | Yes | Later dictionary keys overwrite | Alters the first dictionary | |
Dictionary Unpacking (**) | 3.5+ | No | Later dictionary keys overwrite | Creates a new dictionary | |
| Operand | 3.9+ | No | Later dictionary keys overwrite | Creates a new dictionary | |
collections.ChainMap | - | No | First occurrence is retained | Creates a new ChainMap object |
Additional Details
Custom Merging Logic
For scenarios where you need custom behavior when resolving key conflicts, consider using dictionary comprehension or functions such as functools.reduce.
Performance Considerations
For large dictionaries, consider the performance impact. update(), being in-place, may have better performance compared to creating new structures.
In summary, the method you pick depends on your specific requirements such as performance considerations, Python version in use, and whether you need an immutable result. With this arsenal of techniques, you can efficiently and effectively merge dictionaries in Python.
Related reading
- How do I merge two lists of tuples based on a key?
- How do I modify the background color of a List in SwiftUI?
- How do I pass a scalar via a TensorFlow feed dictionary
- How do I pass a scalar via a TensorFlow feed dictionary
- How do I mock an open used in a with statement using the Mock framework in Python?
- How do I move a file in Python?
- How do I migrate an SVN repository with history to a new Git repository?
- How do I migrate an SVN repository with history to a new Git repository?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.