AppDividend
Latest Code Tutorials

Python combine dicts: How to Merge Two Dictionaries

The dictionary has a key-value pair data structure in Python. There are many scenarios where combining or merging two or multiple dictionaries is essential. In this article, you will see how to combine two or multiple dictionaries.

There are many ways you can combine dictionaries. We will understand the following three ways to combine dicts in Python.

  1. Using dict.update() method
  2. Using unpack(**) operator
  3. Use | operator

Python combine dicts

To combine dicts in Python, use the dict.update() method. The dict.update() is a built-in Python method that updates the dictionary with the items from another dictionary.

If we have two dictionaries and we want to merge them, then we will call the update() method on dictionary 2 and pass dictionary 1 as an argument. It returns None, but dictionary 2 now becomes a merged dictionary.

Syntax of dict.update()

dict.update([iterable])

Arguments

The update() method takes an iterable, which can be either dictionary or any other iterables.

Return Value

The update() method returns None as output.

Implementation of merging two dictionaries

Let’s declare two dictionaries and merge them using the update() method.

book = {"author": "J K Rowling", "book_name": "Harry Potter"}
movie = {"director": "Chris Columbus", "movie_name": "Sorcerers Stone"}

merged_dict = movie.update(book)

print(merged_dict)
print("The Merged Dictionary", movie)

Output

None

The Merged Dictionary {'director': 'Chris Columbus', 'movie_name': 'Sorcerers Stone', 
                       'author': 'J K Rowling', 'book_name': 'Harry Potter'}

You can see that two dictionaries have been merged into one dictionary and the return value of the update() method is None.

So this is the one way you can merge the dictionaries.

Using ** operator to merge dictionary

You can use the single expression ** that allows you to pass multiple arguments to a function directly using a dictionary.

In this example, we will merge two dictionaries using ** expression.

book = {"author": "J K Rowling", "book_name": "Harry Potter"}
movie = {"director": "Chris Columbus", "movie_name": "Sorcerers Stone"}

merged_dict = {**book, **movie}

print(merged_dict)

Output

{'author': 'J K Rowling', 'book_name': 'Harry Potter', 
 'director': 'Chris Columbus', 'movie_name': 'Sorcerers Stone'}

As you can see that the dictionaries have been merged.

Using | operator to merge dictionaries

Use | operator to merge two dictionaries, and it is one of the easiest ways.

book = {"author": "J K Rowling", "book_name": "Harry Potter"}
movie = {"director": "Chris Columbus", "movie_name": "Sorcerers Stone"}

merged_dict = book | movie

print(merged_dict)

Output

{'author': 'J K Rowling', 'book_name': 'Harry Potter', 
 'director': 'Chris Columbus', 'movie_name': 'Sorcerers Stone'}

Merge multiple dictionaries in Python

To merge multiple dictionaries in Python, use the | operator.

book = {"author": "J K Rowling", "book_name": "Harry Potter"}
movie = {"director": "Chris Columbus", "movie_name": "Sorcerers Stone"}
game = {"studio": "Warner Bros", "game_name": "Hogwarts Legacy"}

merged_dict = book | movie | game

print(merged_dict)

Output

{'author': 'J K Rowling', 'book_name': 'Harry Potter', 
 'director': 'Chris Columbus', 'movie_name': 'Sorcerers Stone', 
 'studio': 'Warner Bros', 'game_name': 'Hogwarts Legacy'}

You can see that it merged three dictionaries in just one line code.

That’s it for this tutorial.

See also

Python merge operator

Merge two lists in Python

Python update operator

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.