How to merge multiple Python dictionaries?

In Python, a dictionary is a collection of key-value pairs used for data retrieval. In some cases, we may need to combine two or more dictionaries into one. This is known as merging dictionaries.

Python provides several methods to merge multiple dictionaries, each with different characteristics and use cases.

Using "|" Merge Operator

The "|" merge operator (available in Python 3.9+) merges multiple dictionaries and returns a new dictionary. If both dictionaries have the same key, the value from the right dictionary takes precedence.

Example: Merging Two Dictionaries

The following example merges two dictionaries using the "|" operator ?

# Two sample dictionaries
employee_info = {
    "name": "Niharikaa",
    "department": "Developer"
}

employee_update = {
    "department": "Programming",
    "location": "New York"
}

# Merging using the | operator
merged_dict = employee_info | employee_update

print("Merged Dictionary =", merged_dict)

The output of the above code is ?

Merged Dictionary = {'name': 'Niharikaa', 'department': 'Programming', 'location': 'New York'}

Example: Merging Multiple Dictionaries

You can chain the "|" operator to merge more than two dictionaries ?

# Three dictionaries
dict1 = {"a": 1}
dict2 = {"b": 2}
dict3 = {"a": 3, "c": 4}

# Merging multiple dictionaries
merged_dict = dict1 | dict2 | dict3

print("Merged Dictionary =", merged_dict)

The output of the above code is ?

Merged Dictionary = {'a': 3, 'b': 2, 'c': 4}

Using collections.ChainMap()

The collections.ChainMap() method groups multiple dictionaries into a single view without creating a new dictionary in memory. It searches dictionaries in the order they were passed and returns the value from the first dictionary containing the key.

Example: Creating a Dictionary View

The following example creates a view of two dictionaries using ChainMap ?

from collections import ChainMap

# Two sample dictionaries
employee_info = {
    "name": "Niharikaa",
    "department": "Developer"
}

employee_update = {
    "department": "Programming",
    "location": "New York"
}

# Creating a ChainMap
combined = ChainMap(employee_update, employee_info)

# Converting to a dictionary
merged_dict = dict(combined)

print("Merged Dictionary =", merged_dict)

The output of the above code is ?

Merged Dictionary = {'department': 'Programming', 'location': 'New York', 'name': 'Niharikaa'}

Example: Combining Multiple Dictionaries

This example combines three dictionaries using ChainMap ?

from collections import ChainMap

# Three dictionaries
dict1 = {"a": 1}
dict2 = {"b": 2}
dict3 = {"a": 3, "c": 4}

# Combining using ChainMap
combined = ChainMap(dict3, dict2, dict1)

# Converting the ChainMap to a dictionary
merged_dict = dict(combined)

print("Merged Dictionary =", merged_dict)

The output of the above code is ?

Merged Dictionary = {'a': 3, 'c': 4, 'b': 2}

Using update() Method

The update() method modifies the original dictionary by adding key-value pairs from another dictionary ?

# Two sample dictionaries
dict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "c": 4}

# Using update() method
dict1.update(dict2)

print("Updated Dictionary =", dict1)

The output of the above code is ?

Updated Dictionary = {'a': 1, 'b': 3, 'c': 4}

Using Dictionary Unpacking

Dictionary unpacking with ** creates a new merged dictionary ?

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

# Merging using dictionary unpacking
merged_dict = {**dict1, **dict2, **dict3}

print("Merged Dictionary =", merged_dict)

The output of the above code is ?

Merged Dictionary = {'a': 1, 'b': 2, 'c': 3}

Comparison

Method Python Version Creates New Dict? Best For
| operator 3.9+ Yes Modern Python versions
ChainMap All No (view) Memory-efficient lookups
update() All No (modifies original) In-place modifications
{**dict1, **dict2} 3.5+ Yes Readable syntax

Conclusion

Use the "|" operator for clean syntax in modern Python versions. Use ChainMap for memory-efficient dictionary views, and update() when modifying dictionaries in-place.

Updated on: 2026-03-24T18:49:42+05:30

473 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements