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
Python Program to Concatenate Two Dictionaries Into One
When working with dictionaries in Python, you often need to combine multiple dictionaries into one. Python provides several methods to concatenate dictionaries, each with different behaviors and use cases.
Using the update() Method
The update() method modifies the original dictionary by adding key-value pairs from another dictionary ?
my_dict_1 = {'J': 12, 'W': 22}
my_dict_2 = {'M': 67}
print("The first dictionary is:")
print(my_dict_1)
print("The second dictionary is:")
print(my_dict_2)
my_dict_1.update(my_dict_2)
print("The concatenated dictionary is:")
print(my_dict_1)
The first dictionary is:
{'J': 12, 'W': 22}
The second dictionary is:
{'M': 67}
The concatenated dictionary is:
{'J': 12, 'W': 22, 'M': 67}
Using Dictionary Unpacking (Python 3.5+)
Dictionary unpacking creates a new dictionary without modifying the original ones ?
dict1 = {'A': 10, 'B': 20}
dict2 = {'C': 30, 'D': 40}
# Create new dictionary using unpacking
combined_dict = {**dict1, **dict2}
print("First dictionary:", dict1)
print("Second dictionary:", dict2)
print("Combined dictionary:", combined_dict)
First dictionary: {'A': 10, 'B': 20}
Second dictionary: {'C': 30, 'D': 40}
Combined dictionary: {'A': 10, 'B': 20, 'C': 30, 'D': 40}
Using the Merge Operator (Python 3.9+)
Python 3.9 introduced the merge operator | for dictionary concatenation ?
fruits = {'apple': 5, 'banana': 3}
vegetables = {'carrot': 8, 'lettuce': 2}
# Using merge operator
grocery = fruits | vegetables
print("Fruits:", fruits)
print("Vegetables:", vegetables)
print("Grocery list:", grocery)
Fruits: {'apple': 5, 'banana': 3}
Vegetables: {'carrot': 8, 'lettuce': 2}
Grocery list: {'apple': 5, 'banana': 3, 'carrot': 8, 'lettuce': 2}
Handling Duplicate Keys
When dictionaries have duplicate keys, the second dictionary's values take precedence ?
dict1 = {'name': 'John', 'age': 25, 'city': 'New York'}
dict2 = {'age': 30, 'country': 'USA'}
# Using update() - modifies dict1
dict1_copy = dict1.copy()
dict1_copy.update(dict2)
print("Using update():", dict1_copy)
# Using unpacking - creates new dictionary
merged = {**dict1, **dict2}
print("Using unpacking:", merged)
Using update(): {'name': 'John', 'age': 30, 'city': 'New York', 'country': 'USA'}
Using unpacking: {'name': 'John', 'age': 30, 'city': 'New York', 'country': 'USA'}
Comparison
| Method | Modifies Original? | Python Version | Best For |
|---|---|---|---|
update() |
Yes | All versions | In-place modification |
{**dict1, **dict2} |
No | 3.5+ | Creating new dictionary |
dict1 | dict2 |
No | 3.9+ | Clean, readable syntax |
Conclusion
Use update() to modify existing dictionaries, dictionary unpacking to create new merged dictionaries, or the merge operator for cleaner syntax in Python 3.9+. All methods handle duplicate keys by keeping the last occurrence.
