Skip to content
Home » Merge Multiple lists Of Dictionaries With Same Value In PYTHON

Merge Multiple lists Of Dictionaries With Same Value In PYTHON

  • by
Merge Multiple lists Of Dictionaries

Python, like all other languages, allows programmers to work with arrays. There are 3 types of arrays in python lists, tuples, and dictionaries. Lists have used to store multiple items in single variable Multiple lists Of Dictionaries. Merge multiple lists of dictionaries with the same value in PYTHON.

Tuples are like lists but they 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.

Merge Multiple lists Of Dictionaries With Same Value In PYTHON

A dictionary, as explained above, is a collection that is unordered, changeable, and indexed Multiple lists Of Dictionaries. 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. you can see the sample code below.

from collections import Counter
ini_dictionary1 = Counter({'ali': 1, 'nadeem' : 5, 'zohaib' : 10, 'Ahsaan' : 15})
ini_dictionary2 = Counter({'umer' : 7, 'Ahsaan' : 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 Counter({‘Ahsaan’: 15, ‘zohaib’: 10, ‘nadeem’: 5, ‘ali’: 1})
initial 2nd dictionary Counter({‘m’: 15, ‘umer’: 7, ‘Ahsaan’: 5})
final dictionary Counter({‘Ahsaan’: 20, ‘m’: 15, ‘zohaib’: 10, ‘umer’: 7, ‘nadeem’: 5, ‘ali’: 1})

USING DICT() AND ITEMS.

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

from collections import Counter
ini_dictionary1 = Counter({'ali': 1, 'nadeem' : 5, 'zohaib' : 10, 'Ahsaan' : 15})
ini_dictionary2 = Counter({'umer' : 7, 'Ahsaan' : 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({‘Ahsaan’: 15, ‘zohaib’: 10, ‘nadeem’: 5, ‘ali’: 1})

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

final dictionary Counter({‘Ahsaan’: 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 below you can see.

initial 1st dictionary {'Ahsaan': 15, 'zohaib': 10, 'nadeem': 5, 'ali': 1}
initial 2nd dictionary {'m': 15, 'umer': 7, 'Ahsaan': 5}
final dictionary {'Ahsaan': 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)}

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

Python Program To Convert Hex String To Decimal.

How to Ignore Python Multiple Return Values?

Python creates a directory if it does not exist.

Tags:

Leave a Reply

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