/  Technology   /  Python Merging two Dictionaries
Python Merging two Dictionaries

Python Merging two Dictionaries

There are various ways to merge Dictionaries. In this article we will see few ways to merge two dictionaries and also discuss how to keep the values of common keys instead of overwriting them.

In Python, the Dictionary class provides a function called update() to merge Dictionaries.

 

Syntax:

 

dict.update([other])

 

This function merges the contents of this passed dictionary in the current dictionary.

Let’s see by taking two dictionaries as example and use this update() function to merge two dictionaries.

 

# Create first dictionary
dict1 = { ‘P': 5, 'Q': 7, 'R' : 10 }
# Create second dictionary
dict2 = {'S': 8,'Q': 20,'T' : 11 }

 

let’s try to merge the contents of dict2 in dict1.

 

# Merge contents of dict2 in dict1
dict1.update(dict2)
print('Updated dictionary 1 :')
print(dict1)

 

Output:

 

Python Merging two Dictionaries

 

Here dict1 is added with all the elements in dict2. Basically the dictionary which is passed in update() as argument will override the common key’s values. Therefore ‘Q’ has value 20 now.

 

Another important thing is that, we didn’t get a new dictionary rather the contents of dict1 changed apart from its existing contents it has the contents of dict2 too.

Now let’s see how to merge the contents of both the dictionaries to a new dictionary.

 

Merge two Dictionaries using **kwargs

 

We can send variable length key-value pairs to a function using **kwargs. When we apply ** to a dictionary, it expands the dictionary contents as a collection of key value pairs.

For example, if we have a dictionary as below.

 

dict1 = { 'P': 5, 'Q': 7, 'R' : 10 }

 

It de-serializes the contents of dictionary to a collection of key/value pairs when we apply ** to this dictionary,

 

**dict1

 

is equivalent to,

 

'P': 5, 'Q': 7, 'R' : 10

 

Suppose we have two dictionaries mentioned below.

 

# Create first dictionary
dict1 = {  'P': 5, 'Q': 7, 'R' : 10 }
# Create second dictionary
dict2 = {'S': 8,'Q': 20,'T' : 11 }

 

Below code merges the contents of dict1 and dict2 to a new dictionary dict3 i.e.

 

# Merge contents of dict2 and dict1 into dict3
dict3 = {**dict1 , **dict2}
print('Dictionary 3 :')
print(dict3)

 

Output:

 

Python Merging two Dictionaries

 

**dict1 & **dict2 expands the contents of both the dictionaries to a collection of key value pairs.

Both dict1 & dict2 had one common key ‘Q’. In dict3 value for this common key ‘Q’ is same as in dict2 because we passed the **dict2 as second argument.

 

Merge two dictionaries and keeping the values of common keys

 

In this section we will see how to merge two dictionaries with common key.

Consider two dictionaries with common key as below.

 

# Create first dictionary
dict1 = {  'P': 5, 'Q': 7, 'R' : 10 }
# Create second dictionary
dict2 = {'S': 8,'Q': 20,'T' : 11 }

 

Now we want to merge these two dictionaries, but it should not override the contents and it should keep all the values for common keys in a list.

 

def mergeDict(dict1, dict2):
    dict3 = {**dict1, **dict2}
  for key, value in dict3.items():
    if key in dict1 and key in dict2:
         dict3[key] = [value , dict1[key]]
  return dict3
dict3 = mergeDict(dict1, dict2)
print('Dictionary 3 :')
print(dict3)

 

Output:

 

Python Merging two Dictionaries

 

Hope this article helped you on how to merge two dictionaries using different methods.

Leave a comment