Write a python program to combine two dictionaries adding values for common keys

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

dict1 = {'a': 12, 'for': 25, 'c': 9}

dict2 = {'Geeks': 100, 'geek': 200, 'for': 300}

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}

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

dict1 = {'a': 12, 'for': 25, 'c': 9}

dict2 = {'Geeks': 100, 'geek': 200, 'for': 300}

c = {i: dict1.get[i, 0] + dict2.get[i, 0]

     for i in set[dict1].union[dict2]}

print[c]

Output:

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

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

from collections import Counter

dict1 = {'a': 12, 'for': 25, 'c': 9}

dict2 = {'Geeks': 100, 'geek': 200, 'for': 300}

Cdict = Counter[dict1] + Counter[dict2]

print[Cdict]

Output:

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

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 uisng the chain[].

Python3

import itertools

import collections

dict1 = {'a': 12, 'for': 25, 'c': 9}

dict2 = {'Geeks': 100, 'geek': 200, 'for': 300}

Cdict = collections.defaultdict[int]

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

from functools import reduce

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}

How do you combine two dictionary values for common keys in Python?

Below are the eight standard methods by which you can merge two dictionaries in python..
1] Using update[] method..
2] Using merge[|] operator..
3] Using ** operator..
4] Unpacking the second dictionary..
5] Using collection.ChainMap[] method..
6] Using itertools. ... .
7] Using dictionary comprehension..
8] Add values of common keys..

How do you sum values of two dictionaries in Python?

Merge two dictionaries and sum the values in Python #.
Use a dict comprehension to iterate over one of the dictionaries..
On each iteration, use the dict. get[] method to sum the values..
Specify a default value of 0 in case a key in one dict is not present in the other..

How do you find the common elements in two dictionaries in Python?

Python examples to find common items between 2 or more dictionaries i.e. dictionary intersection items..
Dictionary intersection using '&' operator. Simplest method is to find intersections of keys, values or items is to use & operator between two dictionaries. ... .
Set intersection[] method..

Can we merge two dictionaries in Python?

In the latest update of python now we can use “|” operator to merge two dictionaries. It is a very convenient method to merge dictionaries.

Chủ Đề