I have the following three dictionaries within a list like so:
dict1 = {'key1':'x', 'key2':['one', 'two', 'three']}
dict2 = {'key1':'x', 'key2':['four', 'five', 'six']}
dict3 = {'key1':'y', 'key2':['one', 'two', 'three']}
list = [dict1, dict2, dict3]
I'd like to merge the dictionaries that have the same value for key1 into a single dictionary with merged values (lists in this case) for key2 like so:
new_dict = {'key1':'x', 'key2':['one', 'two', 'three', 'four', 'five', 'six']}
list = [new_dict, dict3]
I've come up with a very brutish solution riddled with hard codes and loops. I'd like to employ some higher-order functions but I'm new to those.
CodePudding user response:
With the help of itertools.groupby and itertools.chain, your goal can be achieved in a single line:
from itertools import groupby
from itertools import chain
dict1 = {'key1':'x', 'key2':['one', 'two', 'three']}
dict2 = {'key1':'x', 'key2':['four', 'five', 'six']}
dict3 = {'key1':'y', 'key2':['one', 'two', 'three']}
list_of_dicts = [dict1, dict2, dict3]
result = [{'key1': k, 'key2': list(chain(*[x['key2'] for x in v]))} for k, v in groupby(list_of_dicts, lambda x: x['key1'])]
print(result)
[{'key1': 'x', 'key2': ['one', 'two', 'three', 'four', 'five', 'six']}, {'key1': 'y', 'key2': ['one', 'two', 'three']}]
CodePudding user response:
Build an intermediate dict that uses key1
as a key to aggregate the key2
lists, and then build the final list of dicts out of that:
>>> my_dicts = [
... {'key1':'x', 'key2':['one', 'two', 'three']},
... {'key1':'x', 'key2':['four', 'five', 'six']},
... {'key1':'y', 'key2':['one', 'two', 'three']},
... ]
>>> agg_dict = {}
>>> for d in my_dicts:
... agg_dict.setdefault(d['key1'], []).extend(d['key2'])
...
>>> [{'key1': key1, 'key2': key2} for key1, key2 in agg_dict.items()]
[{'key1': 'x', 'key2': ['one', 'two', 'three', 'four', 'five', 'six']}, {'key1': 'y', 'key2': ['one', 'two', 'three']}]