1

I have a list of dictionaries like the following:

list_dict = [{"hello": [1, 4, 4, 5, 2], "hi":["sjdgf", "sdlkfjsd", "sdfj", "sdfkj", "sdfkjd"], "namaste":[5, 6, 6, 2, 4]}, {"hello": [1, 4, 4, 5, 2], "hi":["sjdgf", "sdlsdfpjsd", "sdfj", "sdfkj", "sdfkjd"], "namaste":[5, 3, 6, 5, 4]}, {"hello": [1, 4, 4, 5, 2], "hi":["sjsdifjgf", "sdlkfjsd", "sdfj", "sdfkj", "sdfkjd"], "namaste":[5, 3, 6, 17, 4]}]

I want to make a final dictionary which simply merges the lists from each of the dictionaries in list_dict above. The output I am looking for is:

final = {"hello": [1, 4, 4, 5, 2, 1, 4, 4, 5, 2, 1, 4, 4, 5, 2]], "hi":["sjdgf", "sdlkfjsd", "sdfj", "sdfkj", "sdfkjd", "sjdgf", "sdlsdfpjsd", "sdfj", "sdfkj", "sdfkjd", "sjsdifjgf", "sdlkfjsd", "sdfj", "sdfkj", "sdfkjd"], "namaste":[5, 6, 6, 2, 4, 5, 3, 6, 5, 4, 5, 3, 6, 17, 4]}

How can this be done in a scalable way?

Jack Arnestad
  • 1,845
  • 13
  • 26

2 Answers2

2

Using collections.defaultdict

Ex:

import collections
d = collections.defaultdict(list)
list_dict = [{"hello": [1, 4, 4, 5, 2], "hi":["sjdgf", "sdlkfjsd", "sdfj", "sdfkj", "sdfkjd"], "namaste":[5, 6, 6, 2, 4]}, {"hello": [1, 4, 4, 5, 2], "hi":["sjdgf", "sdlsdfpjsd", "sdfj", "sdfkj", "sdfkjd"], "namaste":[5, 3, 6, 5, 4]}, {"hello": [1, 4, 4, 5, 2], "hi":["sjsdifjgf", "sdlkfjsd", "sdfj", "sdfkj", "sdfkjd"], "namaste":[5, 3, 6, 17, 4]}]
for i in list_dict:
    for k, v in i.items():
        d[k].extend(v)
print(d)
print(d["hello"])

Output:

defaultdict(<type 'list'>, {'namaste': [5, 6, 6, 2, 4, 5, 3, 6, 5, 4, 5, 3, 6, 17, 4], 'hi': ['sjdgf', 'sdlkfjsd', 'sdfj', 'sdfkj', 'sdfkjd', 'sjdgf', 'sdlsdfpjsd', 'sdfj', 'sdfkj', 'sdfkjd', 'sjsdifjgf', 'sdlkfjsd', 'sdfj', 'sdfkj', 'sdfkjd'], 'hello': [1, 4, 4, 5, 2, 1, 4, 4, 5, 2, 1, 4, 4, 5, 2]})

[1, 4, 4, 5, 2, 1, 4, 4, 5, 2, 1, 4, 4, 5, 2]
Rakesh
  • 81,458
  • 17
  • 76
  • 113
-1

You can use itertools.groupby:

import itertools, functools
list_dict = [{"hello": [1, 4, 4, 5, 2], "hi":["sjdgf", "sdlkfjsd", "sdfj", "sdfkj", "sdfkjd"], "namaste":[5, 6, 6, 2, 4]}, {"hello": [1, 4, 4, 5, 2], "hi":["sjdgf", "sdlsdfpjsd", "sdfj", "sdfkj", "sdfkjd"], "namaste":[5, 3, 6, 5, 4]}, {"hello": [1, 4, 4, 5, 2], "hi":["sjsdifjgf", "sdlkfjsd", "sdfj", "sdfkj", "sdfkjd"], "namaste":[5, 3, 6, 17, 4]}]
new_list = [i for b in map(dict.items, list_dict) for i in b]
new_d = {a:functools.reduce(lambda x, y:x+y, [c for _, c in b]) for a, b in itertools.groupby(sorted(new_list, key=lambda x:x[0]),key=lambda x:x[0])}

Output:

{'hello': [1, 4, 4, 5, 2, 1, 4, 4, 5, 2, 1, 4, 4, 5, 2], 'hi': ['sjdgf', 'sdlkfjsd', 'sdfj', 'sdfkj', 'sdfkjd', 'sjdgf', 'sdlsdfpjsd', 'sdfj', 'sdfkj', 'sdfkjd', 'sjsdifjgf', 'sdlkfjsd', 'sdfj', 'sdfkj', 'sdfkjd'], 'namaste': [5, 6, 6, 2, 4, 5, 3, 6, 5, 4, 5, 3, 6, 17, 4]}
Ajax1234
  • 69,937
  • 8
  • 61
  • 102