AppDividend
Latest Code Tutorials

How To Combine Two or More Dictionaries In Python

0

Python Dictionary update() is a built-in function that accepts another dictionary or an Iterable object (collection of key-value pairs) as an argument. It merges the contents of this passed dictionary or Iterable in the current dictionary. The update() method did not create a new dictionary; it changes the content of the existing dictionary.

Python Combine Two Dictionaries

There are mainly two ways that we can use the merge two dictionaries in Python.

  1. Merge two or more dictionaries using dict.update()
  2. Merge two or more Dictionaries using **kwargs

Let’s see python combine two dictionaries example.

Merge two or more dictionaries using dict.update()

Python dictionary update() is an inbuilt method that updates one dictionary with the elements of another dictionary object or from an iterable value of key pair. If a key is not present in the dictionary, it first adds the key to the dictionary.

After that, it combines the value; otherwise, if the key is already present in the dictionary, it just updates the value.

In short, it accepts another dictionary or an Iterable object (collection of key-value pairs) as an argument. Then merges the contents of this passed dictionary or Iterable in the current dictionary.

See the following code.

# app.py

dictA = {'asa': 11, 'emma': 19, 'eric' : 21}

dictB = {'millie': 46, 'finn': 23, 'noah' : 18}

dictA.update(dictB)
 
print('Updated dictionary A :')
print(dictA)

Output

Updated dictionary 1 :
{'asa': 11, 'emma': 19, 'eric': 21, 'millie': 46, 'finn': 23, 'noah': 18}

From the output, you can see that All the items from dictB are added to dictA.

In this example, all the keys are from both the dictionaries are unique.

Let’s take an example in which some of the keys are the same for both the dictionaries and see the output.

# app.py

dictA = {'asa': 11, 'emma': 19, 'eric' : 21}

dictB = {'asa': 46, 'finn': 23, 'eric' : 18}

dictA.update(dictB)
 
print('Updated dictionary A :')
print(dictA)

Output

Updated dictionary A :
{'asa': 46, 'emma': 19, 'eric': 18, 'finn': 23}

So, from the output, you can see that keys that are common in both the dictionaries will contain the values as in dictB. The dictionary we are passing in the update() method as an argument will override the standard key’s values. Therefore ‘asa’ has value 46, and ‘eric‘ has value 18 now.

So, the second dictionary’s value will override the first dictionary value, if the keys are the same in both the dictionaries while doing an update().

The update() method did not create a new dictionary(dictA), it updates the content of the first dictionary(dictA). 

Let’s see the second way of how to merge two dictionaries.

Merge two or more Dictionaries using **kwargs

The special syntax **kwargs in python is used to pass the keyworded, variable-length argument list. We use the name kwargs with a double star.

The main reason is that the double star allows us to pass through any number of keyword arguments.

  1. A keyword argument is where you provide the name to the variable as you pass it into the function.
  2. You can think of the kwargs as being the dictionary that maps each keyword to a value that we pass alongside it. That is why when we iterate over the kwargs, there doesn’t seem to be any order in which they were printed out.

For example, if we have a dictionary like the following.

dictA = {'asa': 11, 'emma': 19, 'eric' : 21}

When we apply **(double star) to the dictA, it deserializes the contents of the dictionary to a collection of key-value pairs like the following.

**dictA

Is equivalent to this.

'asa': 11, 'emma': 19, 'eric' : 21

Now, see the following code.

# app.py

dictA = {'asa': 11, 'emma': 19, 'eric' : 21}

dictB = {'millie': 46, 'finn': 23, 'noah' : 18}

dictC = {**dictA, **dictB}
 
print('Combined Dictionary C :')
print(dictC)

In the above code, we are creating the third dictionary and assign the deserialized values of two dictionaries to that dictionaries.

So, we are getting combined dictC, which is the third dictionary, and it does not modify the content of the existing two dictionaries.

Output

Combined Dictionary C :
{'asa': 11, 'emma': 19, 'eric': 21, 'millie': 46, 'finn': 23, 'noah': 18}

The **dictA and **dictB expanded the contents of both the dictionaries to a collection of key-value pairs.

Let’s take three dictionaries and combine all of them and create the fourth dictionary.

# app.py

dictA = {'asa': 11, 'emma': 19, 'eric' : 21}

dictB = {'millie': 46, 'finn': 23, 'noah': 18}

dictC = {'penny': 29, 'sheldon': 36, 'leonard': 2}

dictD = {**dictA, **dictB, **dictC}
 
print('Combined Dictionary D :')
print(dictD)

Output

Combined Dictionary D :
{'asa': 11, 'emma': 19, 'eric': 21, 'millie': 46, 'finn': 23, 'noah': 18, 'penny': 29, 'sheldon': 36, 'leonard': 2}

Conclusion

Merging two or more dictionaries is an easy task in Python. But to find a better approach is a tough choice for every developer. If you do not have any consequences of modifying the existing dictionary, then you can use the dict.update() method otherwise go for **kwargs approach.

Finally, Python combine two dictionaries example article is over.

Leave A Reply

Your email address will not be published.

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