Python dictionaries map hashable keys to values. To merge dictionaries, choose between mutating an existing dictionary with update() and creating a new one using unpacking or the union operator.
There are three common approaches: update an existing dictionary, unpack mappings into a new dictionary, or use the union operator in Python 3.9 and later.
dict.update()modifies an existing dictionary.{**left, **right}creates a new dictionary in Python 3.5+.left | rightcreates a new dictionary in Python 3.9+.
Merge two or more dictionaries using dict.update()
Python Dictionary update() is a built-in function that accepts another dictionary or an iterable object (collection of key-value pairs) as an argument. The update() method doesn’t create a new dictionary; it changes the content of the existing dictionary. The following example merges two dictionaries. The right-hand dictionary wins when a key exists in both.
# Create dictionaries
x = {'a': 11, 'b': 21}
y = {'b': 32, 'c':4}
# Merge contents of y into x
x.update(y)
print('Updated dictionary is :')
print(x)
# Expected output:\n# Updated dictionary is :\n# {'a': 11, 'b': 32, 'c': 4}
In case of duplicate key, update method overwrites old values of the dictionary with the new values of the updating dictionary. So as shown in the above example, the value for key ‘b’ changes from 21 to 32.
Merge dictionaries with unpacking (**)
Inside a dictionary display, ** unpacks the key-value pairs from a mapping. The expression {**x, **y} builds a new dictionary without modifying either input. Dictionary unpacking in displays is supported from Python 3.5 onward.
This is mapping unpacking, not serialization or the **kwargs function-parameter syntax. Both use the double-asterisk notation, but they appear in different contexts.
#Creating dictionaries
x = {'a': 11, 'b': 21}
y = {'b': 32, 'c': 4}
z = {**x, **y}
print("Merged Dictionary: ")
print(z)
# Expected output:\n# Merged Dictionary:\n# {'a': 11, 'b': 32, 'c': 4}
The **x and **y expanded the contents of both the dictionaries to a collection of key-value pairs. Then {} puts it into a new dictionary.
For three or more dictionaries, use {**a, **b, **c} or a | b | c (Python 3.9+). In either expression, the rightmost occurrence of a duplicate key determines its final value.
Updated practical guidance
In Python 3.9+, merged = left | right creates a new dictionary; left |= right updates left in place. The | operator requires dictionaries on both sides, whereas update() accepts another mapping or an iterable of key-value pairs. With duplicate keys, values from the right-hand side take precedence.
left = {"a": 1, "b": 2}
right = {"b": 9, "c": 3}
merged = left | right # Python 3.9+
assert merged == {"a": 1, "b": 9, "c": 3}
assert left == {"a": 1, "b": 2} # Original unchanged
left |= right
assert left == merged
assert left.update({"d": 4}) is None
Accuracy notes and troubleshooting
Unexpected None: dict.update() modifies its receiver and returns None, so do not write merged = left.update(right). Nested values: these are shallow merges; if both inputs have a nested dictionary under the same key, the later nested dictionary replaces the earlier one. Order: Python 3.7+ guarantees dictionary insertion order. Updating an existing key changes its value without moving its position; newly added keys appear at the end. Version errors: the | operator for dictionaries requires Python 3.9 or newer.
Frequently asked questions
Which method works on Python 3.8? Use update() or {**left, **right}; dictionary union with | was introduced in Python 3.9.
How do I merge without changing the originals? Use {**left, **right} or, on Python 3.9+, left | right. Both create a new outer dictionary, but nested mutable objects are still shared.
Can I merge nested dictionaries recursively? Not with these built-in operations alone. A deep merge needs a separately defined rule for conflicting nested values, lists, and other types.
References: Consult the Python dictionary documentation and PEP 584 (dictionary union operators) for exact behavior.
Related guides: How to Create a pandas DataFrame: Lists, Dictionaries, CSV and Excel · Python List Comprehension: Syntax, Filtering and Examples · Python List Slicing Explained: start, stop, step and Examples

C P Gupta is a YouTuber and Blogger. He is expert in Microsoft Word, Excel and PowerPoint. His YouTube channel @pickupbrain is very popular and has crossed 9.9 Million Views.