
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to merge two Python dictionaries in a single expression?
In Python, a Dictionary is a unordered and muttable data structure which stores data as a key-value pair. In some cases, we may need to merge two dictionaries into a single dictionary by using a single expression, in such scenarios python provides different methods.
Using | Merge Operator
The | merge operator in Python is used to merge multiple dictionaries into one with a single expression 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 be replaced with the value from the left dictionary.
Example
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 in a single expression 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'}
Using double asterisk(**) Unpacking Operator
The ** unpacking operator in Python is used to merge multiple dictionaries into one with a single expression and returns the combined dictionary as a new dictionary.
Example
Here is an example, which merges two different dictionaries into one using the ** unpacking operator -
# Two sample dictionaries employee_info = { "name": "Niharikaa", "department": "Developer" } employee_update = { "department": "Programming", "location": "New York" } # Merging using the ** unpacking operator in a single expression merged_dict = {**employee_info, **employee_update} print("Merged Dictionary =", merged_dict)
Here is the output of the above program -
Merged Dictionary = {'name': 'Niharikaa', 'department': 'Programming', 'location': 'New York'}
Using collections.ChainMap() method
The collections.ChainMap() method in Python is used to group multiple dictionaries into one without merging them into a single new dictionary butit resembles to view like 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
Following is the 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)
Below is the output of the above program -
Merged Dictionary = {'name': 'Niharikaa', 'department': 'Programming', 'location': 'New York'}
Using update() Method
The update() method in Python is used to merge one dictionary into another using a single expression. This method updates the calling dictionary with the key-value pairs of the another dictionary. If a key is present in both dictionaries then the value from the dictionary passed as an argument and replace the value in the original dictionary.
Example
Following is the example, which merges two different dictionaries into one using the update() method -
# Two sample dictionaries employee_info = { "name": "Niharikaa", "department": "Developer" } employee_update = { "department": "Programming", "location": "New York" } # Merging using the update() method employee_info.update(employee_update) print("Merged Dictionary =", employee_info)
Following is the output of the above program -
Merged Dictionary = {'name': 'Niharikaa', 'department': 'Programming', 'location': 'New York'}