How to Merge Dictionaries in Python

Merging dictionaries in Python means combining key-value pairs from two or more dictionaries into one.

This page shows the main ways to do that, including what happens when the same key appears in more than one dictionary. It also helps you choose the right method for your Python version and avoid changing a dictionary by mistake.

Quick answer

dict1 = {"name": "Ana", "age": 20}
dict2 = {"city": "Lima", "age": 21}

merged = dict1 | dict2
print(merged)
# {'name': 'Ana', 'age': 21, 'city': 'Lima'}

In Python 3.9+, the | operator merges dictionaries.

If both dictionaries have the same key, the value from the right dictionary replaces the earlier one.

What this page helps you do

  • Combine two dictionaries into one
  • Understand which value is kept when keys are duplicated
  • Choose the best merge method for your Python version
  • Avoid changing the original dictionary by mistake

Merge dictionaries with the | operator

The | operator is the simplest way to merge dictionaries in modern Python.

Use it when:

  • You are using Python 3.9 or later
  • You want a new dictionary
  • You do not want to change the original dictionaries

Example

person = {"name": "Ana", "age": 20}
extra = {"city": "Lima"}

merged = person | extra

print(merged)
print(person)
print(extra)

Output:

{'name': 'Ana', 'age': 20, 'city': 'Lima'}
{'name': 'Ana', 'age': 20}
{'city': 'Lima'}

What to notice

  • merged is a new dictionary
  • person is unchanged
  • extra is unchanged

If the same key exists in both dictionaries, the value on the right side wins:

dict1 = {"age": 20, "city": "Quito"}
dict2 = {"age": 21}

merged = dict1 | dict2
print(merged)

Output:

{'age': 21, 'city': 'Quito'}

If you are still learning how dictionaries work, see Python dictionaries explained.

Merge dictionaries with ** unpacking

** unpacking is another common way to merge dictionaries.

Use it when:

  • You need support for Python 3.5+
  • You want a new dictionary
  • You want to combine multiple dictionaries in one expression

Example

dict1 = {"name": "Ana", "age": 20}
dict2 = {"city": "Lima"}
dict3 = {"country": "Peru"}

merged = {**dict1, **dict2, **dict3}

print(merged)

Output:

{'name': 'Ana', 'age': 20, 'city': 'Lima', 'country': 'Peru'}

Like the | operator, this creates a new dictionary.

Duplicate keys with **

Later dictionaries overwrite earlier ones:

dict1 = {"name": "Ana", "age": 20}
dict2 = {"age": 21, "city": "Lima"}

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

Output:

{'name': 'Ana', 'age': 21, 'city': 'Lima'}

Merge into an existing dictionary with update()

Use update() when you want to change an existing dictionary instead of creating a new one.

This is useful when:

  • You already have a dictionary
  • You want to add more key-value pairs to it
  • You are okay with modifying it directly

Example

person = {"name": "Ana", "age": 20}
extra = {"city": "Lima", "age": 21}

person.update(extra)

print(person)

Output:

{'name': 'Ana', 'age': 21, 'city': 'Lima'}

Important beginner warning

update() changes the original dictionary in place.

That means person itself is modified. It does not create a new dictionary.

If you want to keep the original unchanged, make a copy first:

person = {"name": "Ana", "age": 20}
extra = {"city": "Lima", "age": 21}

merged = person.copy()
merged.update(extra)

print(merged)
print(person)

Output:

{'name': 'Ana', 'age': 21, 'city': 'Lima'}
{'name': 'Ana', 'age': 20}

For more detail, see the Python dictionary update() method.

What happens with duplicate keys

Python dictionaries cannot store the same key twice.

So when you merge dictionaries and the same key appears more than once, one value must replace the other.

Usually:

  • The later dictionary wins
  • The earlier value is overwritten

Small example

dict1 = {"color": "blue", "size": "M"}
dict2 = {"color": "green"}

print(dict1 | dict2)
print({**dict1, **dict2})

Output:

{'color': 'green', 'size': 'M'}
{'color': 'green', 'size': 'M'}

In both cases, "green" replaces "blue" because dict2 comes later.

If you need to keep both values, you cannot do that with the same dictionary key. You would need a different structure, such as a list:

data = {"color": ["blue", "green"]}
print(data)

Output:

{'color': ['blue', 'green']}

How to merge more than two dictionaries

You can merge more than two dictionaries with either multiple | operations or multiple ** expressions.

Using |

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

merged = dict1 | dict2 | dict3
print(merged)

Output:

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

Using **

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

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

Output:

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

Order still matters

If the same key appears more than once, the last value is kept:

dict1 = {"age": 18}
dict2 = {"age": 20}
dict3 = {"age": 22}

merged = dict1 | dict2 | dict3
print(merged)

Output:

{'age': 22}

How to choose the right method

Choose the method based on your Python version and whether you want a new dictionary or not.

Use | when

  • You are using Python 3.9+
  • You want clean, readable syntax
  • You want a new dictionary

Use ** unpacking when

  • You need support for Python 3.5+
  • You want a new dictionary
  • You want to combine several dictionaries in one expression

Use update() when

  • You want to modify an existing dictionary
  • You do not need to keep the original unchanged

A simple rule:

  • Want a new dictionary: use | or **
  • Want to change an existing dictionary: use update()

Common mistakes

These are common problems beginners run into when merging dictionaries.

Using update() without realizing it changes the original dictionary

This is one of the most common mistakes.

dict1 = {"name": "Ana"}
dict2 = {"city": "Lima"}

dict1.update(dict2)
print(dict1)

After this, dict1 is changed.

Expecting Python to keep both values for the same key

This will not happen in a normal dictionary merge:

dict1 = {"age": 20}
dict2 = {"age": 21}

merged = dict1 | dict2
print(merged)

Output:

{'age': 21}

Only one age key can exist.

Using | in an older Python version

If your Python version is older than 3.9, the dictionary | operator will not work.

Check your version with:

python --version

If needed, use:

merged = {**dict1, **dict2}

Merging in the wrong order

Order matters when keys overlap.

dict1 = {"status": "draft"}
dict2 = {"status": "published"}

print(dict1 | dict2)
print(dict2 | dict1)

Output:

{'status': 'published'}
{'status': 'draft'}

FAQ

How do I merge two dictionaries in Python?

Use dict1 | dict2 in Python 3.9+, use {**dict1, **dict2} in Python 3.5+, or use dict1.update(dict2) if you want to change dict1.

Which value is kept when both dictionaries have the same key?

The value from the later or right-side dictionary replaces the earlier one.

Does update() create a new dictionary?

No. update() modifies the existing dictionary.

Can I merge more than two dictionaries?

Yes. You can chain the | operator or use multiple ** unpacking expressions.

Why does my merge method not work with |?

The | operator for dictionaries needs Python 3.9 or later.

See also