[Solved] Merge multiple list of dict with same value in common key [closed]


If you want a more pythonic way:

from itertools import groupby
from pprint import pprint
from collections import ChainMap

a = [{'a':0,'b':23}, {'a':3,'b':77}, {'a':1,'b':99}]
b = [{'a':1,'c':666}, {'a':4,'c':546}]
c = [{'d':33,'a':3}, {'d':1111,'a':4}, {'d':76,'a':1}, {'d':775,'a':0}]
d = [{'a':2,'e':12}, {'a':4,'e':76}]

dict_list = a + b + c + d

# You just need to specify the key you want to use in the lambda function
# There's no need to declare the different key values previously
res = map(lambda dict_tuple: dict(ChainMap(*dict_tuple[1])),
          groupby(sorted(dict_list,
                         key=lambda sub_dict: sub_dict["a"]),
                  key=lambda sub_dict: sub_dict["a"]))

pprint(list(res))

Outputs:

[{'a': 0, 'b': 23, 'd': 775},
 {'a': 1, 'b': 99, 'c': 666, 'd': 76},
 {'a': 2, 'e': 12},
 {'a': 3, 'b': 77, 'd': 33},
 {'a': 4, 'c': 546, 'd': 1111, 'e': 76}]

Edit (Improvement):

You can also use

from _operator import itemgetter
key=itemgetter("a")

instead of

key=lambda sub_dict: sub_dict["a"]

The version with itemgetter is much faster. Using the example you provided:

- Lambda: 0.037109375ms
- Itemgetter: 0.009033203125ms

1

solved Merge multiple list of dict with same value in common key [closed]