prashanth
prashanth

Reputation: 4485

Combine dictionaries based on key value

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.

Upvotes: 0

Views: 121

Answers (4)

Craig Burgler
Craig Burgler

Reputation: 1779

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']}]

Upvotes: 0

mguijarr
mguijarr

Reputation: 7908

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']}]

Upvotes: 1

BPL
BPL

Reputation: 9863

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

Upvotes: 1

pschill
pschill

Reputation: 5569

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.

Upvotes: 2

Related Questions