Python Tutoring Online
 Home

Merge Dictionaries in Python

Merging dictionaries in Python means combining the key-value pairs from two or more dictionaries into a single dictionary. Python offers several ways to do this — the update() method, dictionary unpacking with **, and the union operator | introduced in Python 3.9 — each with different behavior around whether the original dictionaries are modified.

In this tutorial you will learn all three ways to merge dictionaries in Python, how duplicate keys are handled during a merge, which method to use depending on your Python version, and how to merge more than two dictionaries at once.

Need Help Learning Python?

Working with dictionary data structures is an essential Python skill. Beginners often struggle when combining or updating dictionary data.

Learning these concepts with an experienced Python tutor can help you understand real-world programming tasks faster.

Get Python Tutoring Help

How Duplicate Keys Are Handled When Merging

Before looking at each method, it's important to understand one rule that applies to all of them: if the dictionaries being merged share a key, the value from the dictionary that comes later in the merge overwrites the earlier value. No merge method in Python keeps both values or raises an error for duplicate keys — the later value always wins.

Method 1: Using the update() Method

The update() method is the most common way to merge dictionaries. It adds all key-value pairs from one dictionary into another, modifying the original dictionary directly.

Syntax

Python
dictionary1.update(dictionary2)

After executing this statement, the contents of dictionary2 are merged into dictionary1. No new dictionary is created, and dictionary2 itself is left unchanged.

Example

Python
dict1 = {
"a":1,
"b":2
}
dict2 = {
"c":3,
"d":4
}
dict1.update(dict2)
print(dict1)

Output


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

The keys from dict2 are added to dict1, producing a single dictionary containing all key-value pairs from both.

Example: Merging Dictionaries with Duplicate Keys

If both dictionaries contain the same key, the value from the dictionary passed into update() replaces the original value.

Python
dict1 = {
"name":"Alice",
"age":21
}
dict2 = {
"age":22
}
dict1.update(dict2)
print(dict1)

Output


{'name': 'Alice', 'age': 22}

The key age exists in both dictionaries, so the value from dict2 (22) replaces the previous value in dict1 (21).

Method 2: Using Dictionary Unpacking (**)

Python also allows merging dictionaries using dictionary unpacking with the ** operator. Unlike update(), this approach creates a brand new dictionary and leaves both original dictionaries completely unchanged.

Syntax

Python
merged_dict = {**dict1, **dict2}

Example

Python
dict1 = {
"a":1,
"b":2
}
dict2 = {
"c":3,
"d":4
}
merged = {**dict1, **dict2}
print(merged)

Output


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

The ** operator unpacks each dictionary's key-value pairs into a new dictionary literal, combining their elements. This syntax works in Python 3.5 and later, making it the most version-compatible way to create a merged copy.

Method 3: Using the | Operator (Python 3.9+)

Python 3.9 introduced the dictionary union operator, |, as a shorter, more readable way to merge dictionaries into a new dictionary.

Syntax

Python
merged_dict = dict1 | dict2

Example

Python
dict1 = {"a":1,"b":2}
dict2 = {"c":3,"d":4}
merged = dict1 | dict2
print(merged)

Output


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

The | operator merges dictionaries in a concise, readable way and, like unpacking, returns a new dictionary without modifying either original one. It requires Python 3.9 or later.

In-Place Merging with |=

Python 3.9 also introduced |=, which merges a dictionary into an existing one in place, similar to update().

Python
dict1 = {"a":1,"b":2}
dict2 = {"c":3,"d":4}
dict1 |= dict2
print(dict1)

Output


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

dict1 |= dict2 behaves the same way as dict1.update(dict2), modifying dict1 directly rather than creating a new dictionary.

Merging More Than Two Dictionaries

All three methods extend naturally to merging more than two dictionaries at once.

Python
dict1 = {"a":1}
dict2 = {"b":2}
dict3 = {"c":3}

merged = {**dict1, **dict2, **dict3}
print(merged)

merged_with_union = dict1 | dict2 | dict3
print(merged_with_union)

Output


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

Both approaches merge any number of dictionaries in a single expression. If any keys repeat across the dictionaries, the value from the dictionary furthest to the right wins.

Which Merge Method Should You Use?

Time Complexity of Merging Dictionaries

Merging two dictionaries of sizes m and n runs in O(m + n) time with any of these methods, since each key-value pair must be inserted or copied at least once. There is no significant performance difference between update(), unpacking, and the | operator for typical use cases — the choice mainly comes down to whether you want to modify a dictionary in place or create a new one, and which Python version you're targeting.

Common Beginner Mistakes

Frequently Asked Questions

What is the easiest way to merge two dictionaries in Python?

For most modern Python code (3.9+), the union operator dict1 | dict2 is the simplest and most readable way to merge two dictionaries into a new one.

Does merging dictionaries overwrite duplicate keys?

Yes. Whenever the same key appears in more than one dictionary being merged, the value from the dictionary that comes later in the merge replaces any earlier value.

Which merge method works in older versions of Python?

Both update() and dictionary unpacking ({**dict1, **dict2}) work in Python 3.5 and later. The | and |= operators require Python 3.9 or later.

Can I merge more than two dictionaries at once?

Yes. Both unpacking ({**dict1, **dict2, **dict3}) and the union operator (dict1 | dict2 | dict3) support merging any number of dictionaries in a single expression.

Related Python Dictionary Tutorials




Stuck Learning Python?

Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.

Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.

    Get Python Tutoring

PythonTutor.net

Home    Tutorial     Online Live Trainings    Contact Us           © 2026 All rights reserved by www.PythonTutor.net