0
0
PythonProgramBeginner · 2 min read

Python Program to Merge Two Dictionaries

You can merge two dictionaries in Python using the syntax merged = {**dict1, **dict2} or merged = dict1 | dict2 (Python 3.9+).
📋

Examples

Inputdict1 = {'a': 1}, dict2 = {'b': 2}
Output{'a': 1, 'b': 2}
Inputdict1 = {'x': 10, 'y': 20}, dict2 = {'y': 30, 'z': 40}
Output{'x': 10, 'y': 30, 'z': 40}
Inputdict1 = {}, dict2 = {'key': 'value'}
Output{'key': 'value'}
🧠

How to Think About It

To merge two dictionaries, think of combining all key-value pairs from both. If keys repeat, the second dictionary's value replaces the first's. We can do this by unpacking both dictionaries into a new one or using a special operator that joins them.
📐

Algorithm

1
Take the first dictionary.
2
Take the second dictionary.
3
Combine all key-value pairs from both into a new dictionary.
4
If a key exists in both, keep the value from the second dictionary.
5
Return the new merged dictionary.
💻

Code

python
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}

# Method 1: Using dictionary unpacking
merged = {**dict1, **dict2}
print(merged)

# Method 2: Using | operator (Python 3.9+)
merged2 = dict1 | dict2
print(merged2)
Output
{'a': 1, 'b': 3, 'c': 4} {'a': 1, 'b': 3, 'c': 4}
🔍

Dry Run

Let's trace merging dict1 = {'a': 1, 'b': 2} and dict2 = {'b': 3, 'c': 4} using unpacking.

1

Start with dict1

{'a': 1, 'b': 2}

2

Unpack dict1 into new dictionary

New dict = {'a': 1, 'b': 2}

3

Unpack dict2 into new dictionary, overwriting duplicates

New dict = {'a': 1, 'b': 3, 'c': 4}

4

Return merged dictionary

{'a': 1, 'b': 3, 'c': 4}

StepDictionary Content
1{'a': 1, 'b': 2}
2{'a': 1, 'b': 2}
3{'a': 1, 'b': 3, 'c': 4}
4{'a': 1, 'b': 3, 'c': 4}
💡

Why This Works

Step 1: Unpacking dictionaries

Using {**dict1, **dict2} unpacks all key-value pairs from both dictionaries into a new one.

Step 2: Handling duplicate keys

If a key appears in both, the value from the second dictionary overwrites the first, ensuring the latest value is kept.

Step 3: Using | operator

The | operator (Python 3.9+) merges dictionaries similarly, making the code cleaner and easier to read.

🔄

Alternative Approaches

Using dict.update()
python
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
dict3 = dict1.copy()
dict3.update(dict2)
print(dict3)
This modifies a copy of the first dictionary and updates it with the second; it is clear but requires copying.
Using a loop to merge
python
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
merged = dict1.copy()
for k, v in dict2.items():
    merged[k] = v
print(merged)
This manual method is more verbose but helps understand the merging process step-by-step.

Complexity: O(n + m) time, O(n + m) space

Time Complexity

Merging requires visiting all keys in both dictionaries, so time grows with the total number of keys (n and m).

Space Complexity

A new dictionary is created holding all keys and values, so space grows with the total size of both dictionaries.

Which Approach is Fastest?

Using {**dict1, **dict2} or | operator is fastest and most readable; update() is slightly slower due to copying.

ApproachTimeSpaceBest For
Dictionary unpacking {**d1, **d2}O(n + m)O(n + m)Simple, readable merging
| operator (Python 3.9+)O(n + m)O(n + m)Clean syntax, modern Python
dict.update() with copyO(n + m)O(n + m)When modifying a copy explicitly
Manual loopO(n + m)O(n + m)Learning or custom merge logic
💡
Use {**dict1, **dict2} for a quick and readable merge in Python versions before 3.9.
⚠️
Trying to merge dictionaries with + operator, which is not supported and causes errors.