Skip to content

Python merge dictionaries with same value

  • by
merge-dictionaries-with

Python, like all other languages, allows programmers to work with arrays. There are 3 types of arrays in python: lists, tuples, and dictionaries Lists are used to store multiple items in a single variable. Python merge dictionaries with the same value Tuples are like lists but they are ordered and unchangeable.

A dictionary is a collection that is ordered, changeable, and does not allow duplicates. Dictionaries are going to be the main focus of this article so without further ado, let us get straight into it. Python merge dictionaries with the same value

A dictionary, as explained above, is a collection that is unordered, changeable, and indexed. Other than that, we use curly brackets to define a dictionary and they contain pairs of keys and values. Furthermore, dictionaries are used in everyday programming, web development, etc.


Combining dictionaries is a common and frequent task that programmers come by often. Let’s talk about how you can combine the values of two dictionaries having the same key.

 

USING COUNTER

In python, a counter is a subclass of a dictionary and usually performs the same tasks as a dictionary. A sample code is given below for better understanding:

from collections import Counter ini_dictionary1 = Counter({'ali': 1, 'nadeem' : 5, 'zohaib' : 10, 'owais' : 15}) 
ini_dictionary2 = Counter({'umer' : 7, 'owais' : 5, 'm' : 15}) 
print ("initial 1st dictionary", str(ini_dictionary1)) 
print ("initial 2nd dictionary", str(ini_dictionary2)) 
final_dictionary = ini_dictionary1 + ini_dictionary2 
print ("final dictionary", str(final_dictionary))

Output:

initial 1st dictionary {‘owais’: 15, ‘zohaib’: 10, ‘nadeem’: 5, ‘ali’: 1}

initial 2nd dictionary {‘m’: 15, ‘umer’: 7, ‘owais’: 5}

final dictionary {‘owais’: 20, ‘nadeem’: 5, ‘zohaib’: 10, ‘m’: 15, ‘umer’: 7, ‘ali’: 1}

USING DICT() AND ITEMS:

This is another method that you can use to combine two dictionaries. A sample code is given below for your better understanding:

from collections import Counter
ini_dictionary1 = Counter({'ali': 1, 'nadeem' : 5, 'zohaib' : 10, 'owais' : 15})
ini_dictionary2 = Counter({'umer' : 7, 'owais' : 5, 'm' : 15})
print ("initial 1st dictionary", str(ini_dictionary1))
print ("initial 2nd dictionary", str(ini_dictionary2))
final_dictionary = dict(ini_dictionary1.items() + ini_dictionary2.items() + [(k, ini_dictionary1[k] + ini_dictionary2[k])
                    for k in set(ini_dictionary2)
                    set(ini_dictionary1)])
print ("final dictionary", str(final_dictionary))

Output:

initial 1st dictionary Counter({‘owais’: 15, ‘zohaib’: 10, ‘nadeem’: 5, ‘ali’: 1})

initial 2nd dictionary Counter({‘m’: 15, ‘umer’: 7, ‘owais’: 5})

final dictionary Counter({‘owais’: 20, ‘m’: 15, ‘zohaib’: 10, ‘umer’: 7, ‘nadeem’: 5, ‘ali’: 1})


USING DICT COMPREHENSION AND SET:

 

This is the third and final method that you can use to combine dictionaries with the same keys. A sample code for your better understanding is given below:

initial 1st dictionary {'owais': 15, 'zohaib': 10, 'nadeem': 5, 'ali': 1}
initial 2nd dictionary {'m': 15, 'umer': 7, 'owais': 5}
final dictionary {'owais': 20, 'm': 15, 'zohaib': 10, 'umer': 7, 'nadeem': 5, 'ali': 1}
print ("initial 1st dictionary", str(ini_dictionary1))
print ("initial 2nd dictionary", str(ini_dictionary2))
final_dictionary = {x: ini_dictionary1.get(x, 0) + ini_dictionary2.get(x, 0)
                    for x in set(ini_dictionary1).union(ini_dictionary2)}
print ("final dictionary", str(final_dictionary))

Output:
initial 1st dictionary {‘owais’: 15, ‘zohaib’: 10, ‘nadeem’: 5, ‘ali’: 1}
initial 2nd dictionary {‘m’: 15, ‘umer’: 7, ‘owais’: 5}
final dictionary {‘owais’: 20, ‘nadeem’: 5, ‘zohaib’: 10, ‘m’: 15, ‘umer’: 7, ‘ali’: 1}

Read More: The Best Way to Strip Punctuation from a String using Python

Leave a Reply

Your email address will not be published. Required fields are marked *