Open In App

Python | Combine two dictionary adding values for common keys

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given two dictionaries, the task is to combine the dictionaries such that we get the added values for common keys in the resultant dictionary. 
Example: 

Input: dict1 = {'a': 12, 'for': 25, 'c': 9}
       dict2 = {'Geeks': 100, 'geek': 200, 'for': 300}

Output: {'for': 325, 'Geeks': 100, 'geek': 200}

Let’s see some of the methods on How to  Combine two dictionaries by adding values for common keys in Python. 

Naive Method to Combine two dictionary adding values for common keys

Here we are iterating over the dictionaries and adding the values for the same keys.

Python3




# Python program to combine two dictionary
# adding values for common keys
# initializing two dictionaries
dict1 = {'a': 12, 'for': 25, 'c': 9}
dict2 = {'Geeks': 100, 'geek': 200, 'for': 300}
 
 
# adding the values with common key
for key in dict2:
    if key in dict1:
        dict2[key] = dict2[key] + dict1[key]
    else:
        pass
         
print(dict2)


Output:

{'Geeks': 100, 'geek': 200, 'for': 325}

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), to store the keys and values in dictionary.

Using Union Method  to Combine two dictionary adding values for common keys

Here we are using the set union method and with the help of get() function we are fetching the value for that particular keys.

Python3




# initializing dictionaries
dict1 = {'a': 12, 'for': 25, 'c': 9}
dict2 = {'Geeks': 100, 'geek': 200, 'for': 300}
 
# calculating resultant dictionary
c = {i: dict1.get(i, 0) + dict2.get(i, 0)
     for i in set(dict1).union(dict2)}
 
# printing result
print(c)


Output:

{'for': 325, 'a': 12, 'geek': 200, 'Geeks': 100, 'c': 9}

Time complexity: O(n), where n is the total number of keys in both dictionaries. 

Auxiliary space complexity: O(n), where n is the total number of keys in both dictionaries.

Using collections.Counter()  to Combine two dictionary adding values for common keys

Here we are using the collections module to calculate the combination of two dictionaries by adding the values for common keys.

Python3




# Python program to combine two dictionary
# adding values for common keys
from collections import Counter
 
# initializing two dictionaries
dict1 = {'a': 12, 'for': 25, 'c': 9}
dict2 = {'Geeks': 100, 'geek': 200, 'for': 300}
 
 
# adding the values with common key
         
Cdict = Counter(dict1) + Counter(dict2)
print(Cdict)


Output:

Counter({'for': 325, 'geek': 200, 'Geeks': 100, 'a': 12, 'c': 9})

The time complexity of the given program is O(m+n), where m and n are the number of elements in dict1 and dict2, respectively.

The auxiliary space complexity of the program is O(k), where k is the number of unique keys in both dictionaries. 

Using itertools.chain()  to Combine two dictionary adding values for common keys

Here we are using the itertools module to Combine two dictionary by adding values for common keys using the chain().

Python3




# Python program to combine two dictionary
# adding values for common keys
import itertools
import collections
 
# initializing two dictionaries
dict1 = {'a': 12, 'for': 25, 'c': 9}
dict2 = {'Geeks': 100, 'geek': 200, 'for': 300}
 
# using defaultdict
Cdict = collections.defaultdict(int)
 
# iterating key, val with chain()
for key, val in itertools.chain(dict1.items(), dict2.items()):
    Cdict[key] += val
     
print(dict(Cdict))


Output:

{'a': 12, 'for': 325, 'c': 9, 'Geeks': 100, 'geek': 200}

Using functools.reduce and dict comprehension to Combine two dictionary adding values for common keys

Here we are using the functools.reduce() function with the help of dictionary comprehension we are combining two dictionaries.

Python3




#Here is another way to combine dictionaries and
#sum values of common keys (runs fast):
 
 
from functools import reduce
 
# creating three dictionaries in a list
dict_seq = [
  {'a': 1, 'b': 2, 'c': 3},
  {'a':10, 'b': 20},
  {'b': 100},
]
 
print(reduce(lambda d1,d2: {k: d1.get(k,0)+d2.get(k,0)
for k in set(d1)|set(d2)}, dict_seq))


Output:

{'a': 11, 'b': 122, 'c': 3}

The time complexity of this code is O(nk), where n is the number of dictionaries in the ‘dict_seq’ list and k is the total number of keys in all dictionaries combined. 

The auxiliary space complexity of this code is O(k), where k is the total number of keys in all dictionaries combined. 



Last Updated : 27 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads