How to merge multiple Python dictionaries?



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

In this article, we will explore different methods to merge multiple dictionaries in Python.

Using "|" Merge Operator

The "|" merge operator in Python is used to merge multiple dictionaries into one and returns the combined dictionary as a new dictionary.

If both dictionaries have the same key, then the value from the dictionary on the right side of the operator will replace the value from the left dictionary.

Example: Merging two Dictionaries

Following is the example, which merges two different dictionaries into one 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)

Following is the output of the above program -

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

Example: Merging Multiple Dictionaries

In this example, we are merging three different dictionaries into one using the operator "|" -

# 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)

Here is the output of the above program -

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

Using collections.ChainMap() method

The collections.ChainMap() method in Python is used to group multiple dictionaries into one without actually merging them into a single new dictionary, but views them as a single dictionary.

The ChainMap searches each dictionary in the order they were passed and returns the value from the first dictionary that contains the key.

Example: Viewing Multiple Dictionaries as One

Following is an example, which creates a view of two different dictionaries into one by using collections.ChainMap method -

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 view
merged_dict = dict(combined)

print("Merged Dictionary =", merged_dict)

Here is the output of the above program -

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

Example: Combining Multiple Dictionaries

In this example, we are combining three different dictionaries into one dictionary view using collections.ChainMap() method -

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)

Here is the output of the above program -

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

Note: In the above example, the value of key 'a' comes from dict3 since it appears first in the ChainMap. This method does not create a new merged dictionary in memory unless we convert using dict().

Updated on: 2025-06-20T18:57:20+05:30

322 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements