0

How do I combine the rows of dictionaries having same keys. For instance If I have

my_dict_list = [{'prakash': ['confident']},
 {'gagan': ['good', 'luck']},
 {'jitu': ['gold']},
 {'jitu': ['wins']},
 {'atanu': ['good', 'glory']},
 {'atanu': ['top', 'winner','good']}]

My objective is to get

my_new_dict_list = [{'prakash': ['confident']},
 {'gagan': ['good', 'luck']},
 {'jitu': ['gold','wins']},
 {'atanu': ['good', 'glory','top', 'winner','good']}]

How do I do that in Python?

EDIT: The dictionaries in the final list must contain repeated values if present in starting list.

Craig Burgler
  • 1,749
  • 10
  • 19
prashanth
  • 4,197
  • 4
  • 25
  • 42
  • 1
    Possible duplicate of [How can I merge two Python dictionaries in a single expression?](http://stackoverflow.com/questions/38987/how-can-i-merge-two-python-dictionaries-in-a-single-expression) – Ian Aug 29 '16 at 11:05
  • @Prashanth: Why you need list of dicts with single element in each dict. Shouldn't it be single dict? – Moinuddin Quadri Aug 29 '16 at 11:19
  • @MoinuddinQuadri Yes single list is fine as well. I have slightly modified the question. I have to include the terms even if it already appeared in the list. See that 'atanu' key has two values of 'good'. – prashanth Aug 29 '16 at 11:27

4 Answers4

2

You could loop over the dicts in your list and either insert or append the key-value pairs to a new dict:

my_dict_list = [{'prakash': ['confident']},
                {'gagan': ['good', 'luck']},
                {'jitu': ['gold']},
                {'jitu': ['wins']},
                {'atanu': ['good', 'glory']},
                {'atanu': ['top', 'winner']}]


new_d = {}
for d in my_dict_list:
    for k, v in d.items():
        if k in new_d:
            new_d[k] += v
        else:
            new_d[k] = v

Then you need to make a list from the result:

l = [{k: v} for k, v in new_d.items()]
# [{'atanu': ['good', 'glory', 'top', 'winner']}, {'gagan': ['good', 'luck']}, {'prakash': ['confident']}, {'jitu': ['gold', 'wins']}]

You need to be aware that the order of the items in the list may be changed by that.

pschill
  • 5,055
  • 1
  • 21
  • 42
1

Here's a working example:

from itertools import groupby

my_dict_list = [
    {'prakash': ['confident']},
    {'gagan': ['good', 'luck']},
    {'jitu': ['gold']},
    {'jitu': ['wins']},
    {'atanu': ['good', 'glory']},
    {'atanu': ['top', 'winner']}
]

my_new_dict_list = []
for k, g in groupby(my_dict_list, key=lambda x: sorted(x.keys())):
    ds = list(g)
    d = {}
    for k in ds[0].iterkeys():
        d[k] = sum([d[k] for d in ds], [])
    my_new_dict_list .append(d)

print my_new_dict_list
BPL
  • 9,632
  • 9
  • 59
  • 117
1
my_dict_list = [{'prakash': ['confident']},
{'gagan': ['good', 'luck']},
{'jitu': ['gold']},
{'jitu': ['wins']},
{'atanu': ['good', 'glory']},
{'atanu': ['top', 'winner','good']}]

my_new_dict_list = []
tmp_dict = {}
order = []

for d in my_dict_list:
    for k, v in d.iteritems():
        if not k in order: order.append(k)
        tmp_dict.setdefault(k, []).extend(v)

my_new_dict_list = [ {x: tmp_dict[x] } for x in order ]

Output:

[{'prakash': ['confident']},
 {'gagan': ['good', 'luck']},
 {'jitu': ['gold', 'wins']},
 {'atanu': ['good', 'glory', 'top', 'winner', 'good']}]
mguijarr
  • 7,641
  • 6
  • 45
  • 72
0

a minimalist approach using defaultdict:

from collections import defaultdict

my_dict_list = [{'prakash': ['confident']},
 {'gagan': ['good', 'luck']},
 {'jitu': ['gold']},
 {'jitu': ['wins']},
 {'atanu': ['good', 'glory']},
 {'atanu': ['top', 'winner','good']}]

merged_dict = defaultdict(list)
for d in my_dict_list:
    for key, value in d.items():
        merged_dict[key].extend(value)
result = [{key:value} for key, value in merged_dict.items()]

print(result)

Output

[{'prakash': ['confident']}, 
{'gagan': ['good', 'luck']}, 
{'atanu': ['good', 'glory', 'top', 'winner', 'good']}, 
{'jitu': ['gold', 'wins']}]
Craig Burgler
  • 1,749
  • 10
  • 19