DEV Community

Ashutosh Krishna
Ashutosh Krishna

Posted on • Originally published at iread.ga on

How to Merge Dictionaries in Python

Image description

Introduction

In Python, a dictionary is a collection you use to store data in {key:value} pairs. It is ordered and mutable, and it cannot store duplicate data.

We write a dictionary using curly brackets like this:

my_dict = {
    "id": 1,
    "name": "Ashutosh",
    "books": ["Python", "DSA"]
}

Enter fullscreen mode Exit fullscreen mode

Sometimes, we need to merge two or more dictionaries to create a bigger dictionary. For example:

dict_one = {
    "id": 1,
    "name": "Ashutosh",
    "books": ["Python", "DSA"]
}

dict_two = {
    "college": "NSEC",
    "city": "Kolkata",
    "country": "India"
}

merged_dict = {
    "id": 1,
    "name": "Ashutosh",
    "books": ["Python", "DSA"],
    "college": "NSEC",
    "city": "Kolkata",
    "country": "India"
}

Enter fullscreen mode Exit fullscreen mode

In the merged_dict we have the key-value pairs of both dict_one and dict_two. This is what we wish to achieve programmatically.

There are various ways we can do this in Python:

  1. Using for loop
  2. Using the dict.update() method
  3. Using the ** operator
  4. Using the | (Union) operator (for Python 3.9 and above)

Let's explore each way one after another.

1. Using for loop

We can merge two or more dictionaries using for loop like this:

>>> dict_one = {
... "id": 1,
... "name": "Ashutosh",
... }
>>> dict_two = {
... "books": ["Python", "DSA"],
... "college": "NSEC",
... }
>>> dict_three = {
... "city": "Kolkata",
... "country": "India"
... }
>>> for key,value in dict_two.items():
... merged_dict[key] = value
... 
>>> merged_dict
{'id': 1, 'name': 'Ashutosh', 'books': ['Python', 'DSA'], 'college': 'NSEC'}
>>> for key,value in dict_three.items():
... merged_dict[key] = value
... 
>>> merged_dict
{'id': 1, 'name': 'Ashutosh', 'books': ['Python', 'DSA'], 'college': 'NSEC', 'city': 'Kolkata', 'country': 'India'}
Enter fullscreen mode Exit fullscreen mode

But the problem with this method is that we need to run so many loops to merge the dictionaries.

So what's another option?

2. Using dict.update() Method

If you explore the dict class, there are various methods inside it. One such method is the update() method which you can use to merge one dictionary into another.

>>> dict_one = {
... "id": 1,
... "name": "Ashutosh",
... "books": ["Python", "DSA"]
... }
>>> dict_two = {
... "college": "NSEC",
... "city": "Kolkata",
... "country": "India"
... }
>>> dict_one.update(dict_two)
>>> dict_one
{'id': 1, 'name': 'Ashutosh', 'books': ['Python', 'DSA'], 'college': 'NSEC', 'city': 'Kolkata', 'country': 'India'}
Enter fullscreen mode Exit fullscreen mode

But the problem when we use the update() method is that it modifies one of the dictionaries. If we wish to create a third dictionary without modifying any of the other dictionaries, we cannot use this method.

Also, you can only use this method to merge two dictionaries at a time. If you wish to merge three dictionaries, you first need to merge the first two, and then merge the third one with the modified dictionary.

>>> dict_one = {
... "id": 1,
... "name": "Ashutosh",
... }
>>> dict_two = {
... "books": ["Python", "DSA"],
... "college": "NSEC",
... }
>>> dict_three = {
... "city": "Kolkata",
... "country": "India"
... }
>>> dict_one.update(dict_two)
>>> dict_one
{'id': 1, 'name': 'Ashutosh', 'books': ['Python', 'DSA'], 'college': 'NSEC'}
>>> dict_one.update(dict_three)
>>> dict_one
{'id': 1, 'name': 'Ashutosh', 'books': ['Python', 'DSA'], 'college': 'NSEC', 'city': 'Kolkata', 'country': 'India'}
Enter fullscreen mode Exit fullscreen mode

Let's explore some other options.

3. Using the ** operator

You can use the double-asterisk (**) method to unpack or expand a dictionary like this:

>>> dict_one = {
... "id": 1,
... "name": "Ashutosh",
... }
>>> dict_two = {
... "books": ["Python", "DSA"]
... "college": "NSEC",
... }
>>> dict_three = {
... "city": "Kolkata",
... "country": "India"
... }
>>> merged_dict = { **dict_one,** dict_two, **dict_three} 
>>> merged_dict
{'id': 1, 'name': 'Ashutosh', 'books': ['Python', 'DSA'], 'college': 'NSEC', 'city': 'Kolkata', 'country': 'India'}
Enter fullscreen mode Exit fullscreen mode

Using the ** operator to merge the dictionaries doesn't affect any of the dictionaries.

4. Using the | Operator

Starting with Python 3.9, we can use the Union ( | ) operator to merge two or more dictionaries.

>>> dict_one = {
... "id": 1,
... "name": "Ashutosh",
... }
>>> dict_two = {
... "books": ["Python", "DSA"],
... "college": "NSEC",
... }
>>> dict_three = {
... "city": "Kolkata",
... "country": "India"
... }
>>> merged_dict = dict_one | dict_two | dict_three
>>> merged_dict
{'id': 1, 'name': 'Ashutosh', 'books': ['Python', 'DSA'], 'college': 'NSEC', 'city': 'Kolkata', 'country': 'India'}
Enter fullscreen mode Exit fullscreen mode

This is the most convenient method available for merging dictionaries in Python.

Conclusion

We have explored several different methods for merging dictionaries. If you have Python 3.9 or above, you should use the | operator. But if you use older versions of Python, you can still use the other methods discussed above.

Originally published at: https://www.freecodecamp.org/news/merge-dictionaries-in-python/

Top comments (0)