Stupid.Fat.Cat Stupid.Fat.Cat - 16 days ago 10
Python Question

How to merge a list of dictionaries into a dictionary key:list pairing?

I'm trying to merge a list of dictionaries with shared keys into a key:list pairing where the list contains all the values. The bottom code does it, but it's very ugly. I vaguely remember being able to use reduce on a list of dictionaries to accomplish this, but I am at a loss.

1 dictionaries = [{key.split(',')[0]:key.split(',')[1]} for key in open('test.data').read().splitlines()]
2 print dictionaries
3 new_dict = {}
4 for line in open('test.data').read().splitlines():
5 key, value = line.split(',')[0], line.split(',')[1]
6 if not key in new_dict:
7 new_dict[key] = []
8 new_dict[key].append(value)
9 print new_dict


output:

[{'abc': ' a'}, {'abc': ' b'}, {'cde': ' c'}, {'cde': ' d'}]
{'cde': [' c', ' d'], 'abc': [' a', ' b']}


test.data contains:

abc, a
abc, b
cde, c
cde, d

Answer

Your for loop could be simplified using collections.defaultdict as:

from collections import defaultdict 

new_dict = defaultdict(list)

for line in open('test.data').readlines(): # `.readlines()` will work same as
                                           # `.read().splitlines()`
    key, value = line.split(', ')  # <-- unwrapped and automatically assigned
    new_dict[key].append(value)