How To Combine Two Or More Dictionaries In Python with Examples

Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key:value pair. Key value is provided in the dictionary to make it more optimized.

Have you ever wanted to combine two or more dictionaries in Python? There are various ways in which Dictionaries can be merged by the use of various functions and constructors in Python. In this article, we will discuss few ways of merging dictionaries.

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

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

Here are some examples of how to combine two or more dictionaries in Python

Example 1

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.

Example 2

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.

Therefore, a new dictionary dictC is created that contains the data from both the dictionaries.

Here also, if the first and second dictionary has the same keys, then the second dictionary’s value will override the first one.

If you change the order like this.

dictC = {**dictB, **dictA}


Then, the content of dictA will override the content of dictB. So, order matters here.

Example 3

Combine three dictionaries in Python

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}

As you can see 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.

Thanks for reading !

#python #programming

How To Combine Two Or More Dictionaries In Python with Examples
2 Likes10.25 GEEK