Stupid.Fat.Cat Stupid.Fat.Cat - 1 year ago 93
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('').read().splitlines()]
2 print dictionaries
3 new_dict = {}
4 for line in open('').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


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

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

Answer Source

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

from collections import defaultdict 

new_dict = defaultdict(list)

for line in open('').readlines(): # `.readlines()` will work same as
                                           # `.read().splitlines()`
    key, value = line.split(', ')  # <-- unwrapped and automatically assigned
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download