user1717931
user1717931

Reputation: 2501

merge two lists of dicts

Suppose I have two lists of dictionaries. The keys are identical in name and number (of keys). But, the values are either same or they differ. Output: a list of dicts where the values are merged.

list_a = [{'token_a': ['ab', 'cd', 'ef', 'hello', 'there']}, {'token_b': ['abc', 'rcd', 'gef', 'more', 'values']}]
list_b = [{'token_a': ['ab', 'cd', 'dfcdef']}, {'token_b': ['abc', 'rcd', 'jdhfgef']}]

expected output:

output_list = [{'token_a': ['ab', 'cd', 'ef', 'dfcdef', 'hello', 'there']}, {'token_b': ['abc', 'rcd', 'gef', 'jdhfgef', 'more', 'values']}]

This is what I tried: - def a fn that merges a pair of dicts - then, run this fn in a for-loop for each pair (from the two lists).

def merge_pair_of_dicts(d1, d2):
    final_values = []
    merged_dict = {}
    for k, v1 in d1.items():
        for _, v2 in d2.items():
            values = [v1 + v2]
            values = [item for sublist in values for item in sublist]
            final_values = list(set(values))
        merged_dict = {k: final_values}
    return merged_dict


zipped_lists = list(zip(list_a, list_b))
print(zipped_lists)

final_list_of_dicts = []
for dict_pair in zipped_lists:
    d1 = dict_pair[0]
    d2 = dict_pair[1]
    merged_dict = merge_pair_of_dicts(d1, d2)
    final_list_of_dicts.append(merged_dict)

print(final_list_of_dicts)

My above procedure is readable and will do the job. But, is there a nicer way that can merge two lists of dict?

Upvotes: 1

Views: 253

Answers (3)

pythonic833
pythonic833

Reputation: 3224

That's a good case for defaultdict.

from collections import defaultdict
dd = defaultdict(set)

for dct in  list_a + list_b:
    for key,value in dct.items():
        dd[key] = list(set(dd[key]).union(set(value)))

result

defaultdict(set,
            {'token_a': ['ef', 'cd', 'dfcdef', 'ab'],
             'token_b': ['jdhfgef', 'gef', 'abc', 'rcd']})

Upvotes: 2

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

In a single list comprehension and set object:

list_a = [{'token_a': ['ab', 'cd', 'ef', 'hello', 'there']}, {'token_b': ['abc', 'rcd', 'gef', 'more', 'values']}]
list_b = [{'token_a': ['ab', 'cd', 'dfcdef']}, {'token_b': ['abc', 'rcd', 'jdhfgef']}]

res= [{k: list(set(v + list_b[i][k]))}
       for i, d in enumerate(list_a) for k, v in d.items()]
print(res)

The output:

[{'token_a': ['ab', 'there', 'dfcdef', 'cd', 'ef', 'hello']}, {'token_b': ['more', 'jdhfgef', 'abc', 'values', 'gef', 'rcd']}]

Upvotes: 3

Oli
Oli

Reputation: 1393

One liner is

[{k: list(set(v+b.values()[0]))} for a in list_a for b in list_b for k,v in a.iteritems() if k in b.keys()]

result:

[{'token_a': ['dfcdef', 'ab', 'ef', 'cd']},
 {'token_b': ['rcd', 'abc', 'gef', 'jdhfgef']}]

Upvotes: 0

Related Questions