Merge two dictionaries together using python (3 ways)

featured Image

In this article, we will learn how to merge two or more dictionaries into one using python with some examples.

In Python, dictionaries are one of the most used data structures. It is used to hold data in a key-value pair. And some times while working with dictionaries we might want to combine or merge two dictionaries together to update our data.

So, here we will see and learn what are the ways to combine two or more dictionaries in python programming.

What is Dictionary in Python?

A dictionary is a data type that holds data in a key-value pair. Python dictionary is a collection that is ordered, changeable, and does not allow any duplicates. That means it does not allow items with the same key.

Example of a dictionary in python.

dict = { "name":"Alex","age":"24" }

Merge two dictionaries into one in python

In python, we can merge or combine two or more dictionaries together using:

  1. Using | operator
  2. Using update() method
  3. Using the ** Operator

Let’s see each method with examples.

Using |(merge) operation to merge dictionaries

We can fuse two or more dictionaries into one in python by using the merge (|) operator.

It creates a new dictionary with all the merge items leaving the two merged dictionaries unchanged.

Here, is a demonstration of merging dictionaries into a new one using | operator.

person = { 
    "name": "John",
    "country" : "USA"
}
person_details = { 
    "phone-no": "555-555-5551",
    "state" : "New York"
}

new_dict = person | person_details
print(new_dict)

Output:

{'name': 'John', 'country': 'USA', 'phone-no': '555-555-5551', 'state': 'New York'}

Note: We can only use the merge (|) operator in python 3.9 or higher.

Using update() method to merge two dictionaries

The update() method is used to insert items in a dictionary in python. The inserted item can be a dictionary or any iterable object with key-value pairs.

Example of update method to combine two dictionaries together.

person = { 
    "name": "John",
    "country" : "Canada"
}
person_details = { 
    "state" : "New York",
    "country": "USA"
}

person.update(person_details)
print(person)

Output:

{'name': 'John', 'country': 'USA', 'state': 'New York'}

Here, we have merged the person_details dictionary with the person dictionary using the update() method.

Python dictionaries don’t allow items with the same key ie duplicate items, so if we have an item with the same key (country) with different values in both dictionaries, the dictionary gets updated with the later key-value pair.

Merge two or more dictionaries with ** operator

This is one of the easy and simple methods to merge two or more dictionaries together in python.

We used ** operator to unpack dictionaries in python. It expands the content of the dictionaries and combines it together to create a new dictionary.

Example:

person = { 
    "name": "John",
    "country" : "Canada"
}
person_details = { 
    "state" : "New York",
    "country": "USA"
}

new_dict = { **person, **person_details }

print(new_dict)

Output:

{'name': 'John', 'country': 'USA', 'state': 'New York'}

If you have more than two dictionaries, you can easily combine all of them together using the unpacking ** operator in python.

Conclusion: Here, we have learned 3 different ways to combine two or multiple dictionaries together in python.


Related Topics:

Merge multiple JSON objects into one single object in Python

Delete an Element (key) From a Python Dictionary

Related Posts

merge json files in python

How to merge JSON files in python

JSON (JavaScript Object Notation) is a data format used for storing, transferring, and visualizing data. Its simple structure of key:value pairs and array data types makes JSON an easy and…

Read more
Install python pip in ubuntu

How to Install Pip(pip3) in Ubuntu Linux 22.04

Pip is a package manager for Python that allows you to install and manage additional Python packages that are not part of the Python standard library. Pip makes it easy…

Read more
featured Image

How to Write If-Else Statements in One Line in Python

If-else statements are a fundamental concept in Python and other programming languages. They allow you to execute different blocks of code based on whether a condition is true or false….

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *