[Solved] How to merge list of dictionaries in python in shortest and fastest way possible?


One of the shortest way would be to

  • prepare a list/set of all the keys from all the dictionaries
  • and call that key on all the dictionary in the list.

list_of_dict = [{'a': 1, 'b': 2, 'c': 3}, {'a': 3, 'b': 5}, {'k': 5, 'j': 5}, {'a': 3, 'k': 5, 'd': 4}, {'a': 3}]

# prepare a list/set of all the keys from all the dictionaries

# method 1: use sum 
all_keys = sum([[a for a in x.keys()] for x in list_of_dict], [])

# method 2: use itertools 
import itertools
all_keys = list(itertools.chain.from_iterable(list_of_dict))

# method 3: use union of the set
all_keys = set().union(*list_of_dict)

print(all_keys)
# ['a', 'b', 'c', 'a', 'b', 'k', 'j', 'a', 'k', 'd', 'a']

# convert the list to set to remove duplicates 
all_keys = set(all_keys)
print(all_keys)
# {'a', 'k', 'c', 'd', 'b', 'j'}

# now merge the dictionary
merged = {k: [d.get(k) for d in list_of_dict if k in d] for k in all_keys}
print(merged)
# {'a': [1, 3, 3, 3], 'k': [5, 5], 'c': [3], 'd': [4], 'b': [2, 5], 'j': [5]}

In short:

all_keys = set().union(*list_of_dict)
merged = {k: [d.get(k) for d in list_of_dict if k in d] for k in all_keys}

print(merged)
# {'a': [1, 3, 3, 3], 'k': [5, 5], 'c': [3], 'd': [4], 'b': [2, 5], 'j': [5]}

2

solved How to merge list of dictionaries in python in shortest and fastest way possible?