Change language

Python | Combine the values ​​of two dictionaries that have the same key

| |

Let’s see how to combine the values ​​of two dictionaries that have the same key.

Method # 1: Using Counter
Counter — it is a special subclass of a dictionary that in most cases does the same thing as a dictionary.

# Python code to demonstrate merging
# two dictionaries with the same key

 

from collections import Counter

 
# initializing dictionaries

ini_dictionary1 = Counter ({ ’ nikhil’ : 1 , ’akash’ : 5 ,

’manjeet’ : 10 , ’akshat’ : 15 })

ini_dictionary2 = Counter ({ ’akash’ : 7 , ’akshat’ : 5 ,

’m’ : 15 })

  
# print source dictionaries

print ( "initial 1st dictionary" , str (ini_dictionary1))

print ( "initial 2nd dictionary" , str (ini_dictionary2))

 
# merging dictionaries
# using counter

final_dictionary = ini_dictionary1 + ini_dictionary2

 
# print the final result

print ( "final dict ionary " , str (final_dictionary))

Exit:

initial 1st dictionary Counter ({ ’akshat’: 15, ’manjeet’: 10, ’akash’: 5, ’nikhil’: 1})
initial 2nd dictionary Counter ({’m’: 15, ’akash’: 7, ’akshat’ : 5})
final dictionary Counter ({’akshat’: 20, ’m’: 15, ’akash’: 12, ’manjeet’: 10, ’nikhil’: 1})

Method # 2: Using dict () and items
This method is for Python version 2.

# Python code to demonstrate merging
# two dictionaries with the same key

 

 
# initializing dictionaries

ini_dictionary1 = { ’nikhil’ : 1 , ’akash’ : 5 ,

’manjeet’ : 10 , ’akshat’ : 15 }

ini_dictionary2 = { ’akash’ : 7 , ’ akshat’ : 5 ,

  ’m’ : 15 }

 
# print the original dictionaries

print ( "initial 1st dictionary " , str (ini_dictionary1))

print ( "initial 2nd dictionary" , str (ini_dictionary2))

 
# merging dictionaries
# using dict () and items ()

final_dictionary = dict (ini_dictionary1.items () + ini_dictionary2.items () +

[(k , ini_dictionary1 [k] + ini_dictionary2 [k])

for k in set (ini_dictionary2)

& amp;  set (ini_dictionary1)])

 
# print the final result

print ( " final dictionary " , str (final_dictionary))

Output:

(’initial 1st dictionary’, “{’manjeet’: 10, ’nikhil’: 1, ’akshat’: 15, ’akash’: 5}”)
(’initial 2nd dictionary’, “{’m’: 15, ’akshat’: 5, ’akash’: 7}”)
(’final dictionary’, “{’nikhil’: 1, ’m’ : 15, ’manjeet’: 10, ’akshat’: 20, ’akash’: 12} ”)

Method # 3: Using dictation and understanding

# Python code for demo combining comments
# two dictionaries with the same key

 
# initializing dictionaries

ini_dictionary1 = { ’nikhil’ : 1 , ’akash’ : 5

  ’ manjeet ’ : 10 , ’ akshat’ : 15 }

ini_dictionary2 = { ’ akash’ : 7 , ’ akshat’ : 5

  ’ m’ : 15 }

 
# print source dictionaries

print ( "initial 1st dictionary" , str (ini_dictionary1))

print ( "initial 2nd dictionary" , str (ini_dictionary2))

  
# merging dictionaries
# using word comprehension and setting

final_dictionary =   {x: ini_dictionary1.get (x, 0 ) + ini_dictionary2.get (x, 0 )

for x in set ( ini_dictionary1) .union (ini_dictionary2)}

 
# print the final result

print ( "final dictionary" , str (final_dictionary))

Output:

initial 1st dictionary {’nikhil’: 1, ’akshat’: 15, ’akash’: 5, ’manjeet’: 10}
initial 2nd dictionary {’akshat’: 5, ’ akash ’: 7,’ m ’: 15}
final dictionary {’ nikhil ’: 1,’ akshat ’: 20,’ akash ’: 12,’ m ’: 15,’ manjeet ’: 10}