|
| Home |
|---|
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.
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.
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.
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.
dict1 = {
"a":1,
"b":2
}
dict2 = {
"c":3,
"d":4
}
dict1.update(dict2)
print(dict1)
{'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.
If both dictionaries contain the same key, the value from the
dictionary passed into update() replaces the original
value.
dict1 = {
"name":"Alice",
"age":21
}
dict2 = {
"age":22
}
dict1.update(dict2)
print(dict1)
{'name': 'Alice', 'age': 22}
The key age exists in both dictionaries, so the value
from dict2 (22) replaces the previous
value in dict1 (21).
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.
merged_dict = {**dict1, **dict2}
dict1 = {
"a":1,
"b":2
}
dict2 = {
"c":3,
"d":4
}
merged = {**dict1, **dict2}
print(merged)
{'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.
Python 3.9 introduced the dictionary union operator,
|, as a shorter, more readable way to merge dictionaries
into a new dictionary.
merged_dict = dict1 | dict2
dict1 = {"a":1,"b":2}
dict2 = {"c":3,"d":4}
merged = dict1 | dict2
print(merged)
{'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.
Python 3.9 also introduced |=, which merges a
dictionary into an existing one in place, similar to
update().
dict1 = {"a":1,"b":2}
dict2 = {"c":3,"d":4}
dict1 |= dict2
print(dict1)
{'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.
All three methods extend naturally to merging more than two dictionaries at once.
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)
{'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.
update() on Python 3.9+.
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.
update() returns a new dictionary, when it actually modifies the original in place and returns None| or |= union operator in Python versions earlier than 3.9, which raises a TypeError
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.
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.
Both update() and dictionary unpacking
({**dict1, **dict2}) work in Python 3.5 and later. The
| and |= operators require Python 3.9 or
later.
Yes. Both unpacking ({**dict1, **dict2, **dict3}) and
the union operator (dict1 | dict2 | dict3) support
merging any number of dictionaries in a single expression.
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.
Home Tutorial Online Live Trainings Contact Us © 2026 All rights reserved by www.PythonTutor.net