Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to merge two Python dictionaries in a single expression?
In Python, a Dictionary is an unordered and mutable data structure which stores data as key-value pairs. In some cases, we may need to merge two dictionaries into a single dictionary using a single expression. Python provides several methods to accomplish this task efficiently.
Using | Merge Operator (Python 3.9+)
The | merge operator is the most modern and readable way to merge dictionaries in a single expression. It returns a new dictionary containing all key-value pairs from both dictionaries. If both dictionaries have the same key, the value from the right-side dictionary takes precedence ?
# Two sample dictionaries
employee_info = {
"name": "Niharikaa",
"department": "Developer"
}
employee_update = {
"department": "Programming",
"location": "New York"
}
# Merging using the | operator in a single expression
merged_dict = employee_info | employee_update
print("Merged Dictionary =", merged_dict)
Merged Dictionary = {'name': 'Niharikaa', 'department': 'Programming', 'location': 'New York'}
Using Double Asterisk (**) Unpacking Operator
The ** unpacking operator provides a concise way to merge dictionaries in a single expression. This method works in Python 3.5+ and creates a new dictionary by unpacking all key-value pairs ?
# Two sample dictionaries
employee_info = {
"name": "Niharikaa",
"department": "Developer"
}
employee_update = {
"department": "Programming",
"location": "New York"
}
# Merging using the ** unpacking operator in a single expression
merged_dict = {**employee_info, **employee_update}
print("Merged Dictionary =", merged_dict)
Merged Dictionary = {'name': 'Niharikaa', 'department': 'Programming', 'location': 'New York'}
Using collections.ChainMap() Method
The collections.ChainMap() method groups multiple dictionaries into a single view without creating a new dictionary. ChainMap searches each dictionary in order and returns the value from the first dictionary containing the key ?
from collections import ChainMap
# Two sample dictionaries
employee_info = {
"name": "Niharikaa",
"department": "Developer"
}
employee_update = {
"department": "Programming",
"location": "New York"
}
# Creating a ChainMap and converting to dictionary
combined = ChainMap(employee_update, employee_info)
merged_dict = dict(combined)
print("Merged Dictionary =", merged_dict)
Merged Dictionary = {'department': 'Programming', 'location': 'New York', 'name': 'Niharikaa'}
Using update() Method
The update() method modifies the original dictionary by adding key-value pairs from another dictionary. This method doesn't create a new dictionary but updates the existing one in place ?
# Two sample dictionaries
employee_info = {
"name": "Niharikaa",
"department": "Developer"
}
employee_update = {
"department": "Programming",
"location": "New York"
}
# Merging using the update() method
employee_info.update(employee_update)
print("Merged Dictionary =", employee_info)
Merged Dictionary = {'name': 'Niharikaa', 'department': 'Programming', 'location': 'New York'}
Comparison
| Method | Python Version | Creates New Dict? | Single Expression? |
|---|---|---|---|
| operator |
3.9+ | Yes | Yes |
** unpacking |
3.5+ | Yes | Yes |
ChainMap |
All | No (view) | Yes |
update() |
All | No (modifies) | Yes |
Conclusion
Use the | operator for the most readable syntax in Python 3.9+, or ** unpacking for older versions. Use update() when you want to modify the original dictionary, and ChainMap for memory-efficient dictionary views.
