merge values of same key, in list of dicts

  • Last Update :
  • Techknowledgy :

The simplest way to merge two dictionaries in python is by using the unpack operator(**). By applying the "**" operator to the dictionary, it expands its content being the collection of key-value pairs.,We can combine two dictionaries in python using dictionary comprehension. Here, we also use the for loop to iterate through the dictionary items and merge them to get the final output. If both the dictionaries have common keys, then the final output using this method will contain the value of the second dictionary. Check out the below example for a better understanding.,Unpacking the second dictionary using the "**" operator will help you merge two dictionaries and get your final output. However, this method is not highly used and recommended because it will only work if the keys of the second dictionary are compulsorily strings. If any int value is encountered, then it will raise the "TypeError" method.,As dictionary is also iterable, we can use the chain() function from itertools class and merge two dictionaries. The return type of this method will be an object, and hence, we can convert the dictionary using the dict() constructor.

sample_dict = {
   "Language_1": "Python",
   "Langauge_2": "C++",
   "Language_3": "Java"
}
print(sample_dict)
{
   'Language_1': 'Python',
   'Langauge_2': 'C++',
   'Language_3': 'Java'
}
dict_1 = {
   'John': 15,
   'Rick': 10,
   'Misa': 12
}
dict_2 = {
   'Bonnie': 18,
   'Rick': 20,
   'Matt': 16
}
dict_1.update(dict_2)
print('Updated dictionary:')
print(dict_1)
{
   'John': 15,
   'Rick': 20,
   'Misa': 12,
   'Bonnie': 18,
   'Matt': 16
}
def Merge(dict_1, dict_2):
   result = dict_1 | dict_2
return result

# Driver code
dict_1 = {
   'John': 15,
   'Rick': 10,
   'Misa': 12
}
dict_2 = {
   'Bonnie': 18,
   'Rick': 20,
   'Matt': 16
}
dict_3 = Merge(dict_1, dict_2)
print(dict_3)
dict_1 = {
   'John': 15,
   'Rick': 10,
   'Misa': 12
}
print(dict( ** dict_1))

Suggestion : 2

Here's a general solution that will handle an arbitrary amount of dictionaries, with cases when keys are in only some of the dictionaries:

from collections
import defaultdict

d1 = {
   1: 2,
   3: 4
}
d2 = {
   1: 6,
   3: 7
}

dd = defaultdict(list)

for d in (d1, d2): # you can list as many input dicts as you want here
for key, value in d.items():
   dd[key].append(value)

print(dd)

Shows:

defaultdict(<type 'list'>, {1: [2, 6], 3: [4, 7]})

assuming all keys are always present in all dicts:

ds = [d1, d2]
d = {}
for k in d1.iterkeys():
   d[k] = tuple(d[k]
      for d in ds)

Note: In Python 3.x use below code:

ds = [d1, d2]
d = {}
for k in d1.keys():
   d[k] = tuple(d[k]
      for d in ds)

and if the dic contain numpy arrays:

ds = [d1, d2]
d = {}
for k in d1.keys():
   d[k] = np.concatenate(list(d[k]
      for d in ds))
1._
dict1 = {
   'm': 2,
   'n': 4
}
dict2 = {
   'n': 3,
   'm': 1
}

Making sure that the keys are in the same order:

dict2_sorted = {
   i: dict2[i]
   for i in dict1.keys()
}

keys = dict1.keys()
values = zip(dict1.values(), dict2_sorted.values())
dictionary = dict(zip(keys, values))

gives:

{
   'm': (2, 1),
   'n': (4, 3)
}

If you only have d1 and d2,

from collections
import defaultdict

d = defaultdict(list)
for a, b in d1.items() + d2.items():
   d[a].append(b)

Here is one approach you can use which would work even if both dictonaries don't have same keys:

d1 = {
   'a': 'test',
   'b': 'btest',
   'd': 'dreg'
}
d2 = {
   'a': 'cool',
   'b': 'main',
   'c': 'clear'
}

d = {}

for key in set(d1.keys() + d2.keys()):
   try:
   d.setdefault(key, []).append(d1[key])
except KeyError:
   pass

try:
d.setdefault(key, []).append(d2[key])
except KeyError:
   pass

print d

This would generate below input:

{
   'a': ['test', 'cool'],
   'c': ['clear'],
   'b': ['btest', 'main'],
   'd': ['dreg']
}

This function merges two dicts even if the keys in the two dictionaries are different:

def combine_dict(d1, d2):
   return {
      k: tuple(d[k]
         for d in (d1, d2) if k in d)
      for k in set(d1.keys()) | set(d2.keys())
   }

Example:

d1 = {
   'a': 1,
   'b': 2,
}
d2` = {
    'b': 'boat',
    'c': 'car',
}
combine_dict(d1, d2)
# Returns: {
#    'a': (1,),
#    'b': (2, 'boat'),
#    'c': ('car',)
# }

Suggestion : 3

Last Updated : 19 Jan, 2022

The original list is: [{
   'gfg': [1, 5, 6, 7],
   'good': [9, 6, 2, 10],
   'CS': [4, 5, 6]
}, {
   'gfg': [5, 6, 7, 8],
   'CS': [5, 7, 10]
}, {
   'gfg': [7, 5],
   'best': [5, 7]
}]
The concatenated dictionary: {
   'gfg': [1, 5, 6, 7, 5, 6, 7, 8, 7, 5],
   'good': [9, 6, 2, 10],
   'CS': [4, 5, 6, 5, 7, 10],
   'best': [5, 7]
}

Original List: [{
   'gfg': [1, 5, 6, 7],
   'good': [9, 6, 2, 10],
   'CS': [4, 5, 6]
}, {
   'gfg': [5, 6, 7, 8],
   'CS': [5, 7, 10]
}, {
   'gfg': [7, 5],
   'best': [5, 7]
}]
Concatenated Dictionary: {
   'gfg': [1, 5, 6, 7, 5, 6, 7, 8, 7, 5],
   'good': [9, 6, 2, 10],
   'CS': [4, 5, 6, 5, 7, 10],
   'best': [5, 7]
}

Suggestion : 4

To merge multiple dictionaries, the most Pythonic way is to use dictionary comprehension {k:v for x in l for k,v in x.items()} to first iterate over all dictionaries in the list l and then iterate over all (key, value) pairs in each dictionary.,You can use dictionary comprehension {k:v for x in l for k,v in x.items()} to first iterate over all dictionaries in the list l and then iterate over all (key, value) pairs in each dictionary. ,This is the most Pythonic way to merge multiple dictionaries into a single one and it works for an arbitrary number of dictionaries.,Go over all dictionaries in the list of dictionaries l by using the outer loop for x in l.

Problem: Say, you’ve got a list of dictionaries:

[{
   'a': 0
}, {
   'b': 1
}, {
   'c': 2
}, {
   'd': 3
}, {
   'e': 4,
   'a': 4
}]

How do you merge all those dictionaries into a single dictionary to obtain the following one?

{
   'a': 4,
   'b': 1,
   'c': 2,
   'd': 3,
   'e': 4
}
  • Create a new dictionary using the {...} notation.
  • Go over all dictionaries in the list of dictionaries l by using the outer loop for x in l.
  • Go over all (key, value) pairs in the current dictionary x by using the x.items() method that returns an iterable of (key, value) pairs.
  • Fill the new dictionary with (key, value) pairs k:v using the general dictionary comprehension syntax {k:v for ...}.
l = [{
   'a': 0
}, {
   'b': 1
}, {
   'c': 2
}, {
   'd': 3
}, {
   'e': 4,
   'a': 4
}]

d = {
   k: v
   for x in l
   for k,
   v in x.items()
}
print(d)
# {
   'a': 4,
   'b': 1,
   'c': 2,
   'd': 3,
   'e': 4
}
  • Create a new, empty dictionary.
  • Go over each dictionary in the list of dictionaries.
  • Update the new dictionary with the values in the current dictionary.
l = [{
   'a': 0
}, {
   'b': 1
}, {
   'c': 2
}, {
   'd': 3
}, {
   'e': 4,
   'a': 4
}]

d = {}
for dictionary in l:
   d.update(dictionary)

print(d)
# {
   'a': 4,
   'b': 1,
   'c': 2,
   'd': 3,
   'e': 4
}

Side note: Sometimes it’s also used for keyword arguments.

l = [{
   'a': 0
}, {
   'b': 1
}, {
   'c': 2
}, {
   'd': 3
}, {
   'e': 4,
   'a': 4
}]

d = {
   ** l[0],
   ** l[1],
   ** l[2],
   ** l[3],
   ** l[4]
}
print(d)
# {
   'a': 4,
   'b': 1,
   'c': 2,
   'd': 3,
   'e': 4
}