Combining the values in two dictionaries into a list
dictionary, list, python
Solution
from collections import Counter
c1 = Counter({'item1': 4, 'item2':2, 'item3': 5, 'item4': 3})
c2 = Counter({'item1': 6, 'item2':2, 'item3': 1, 'item5': 9})
c3 = {}
for c in (c1, c2):
for k,v in c.iteritems():
c3.setdefault(k, []).append(v)
`c3` is now: `{'item1': [4, 6], 'item2': [2, 2], 'item3': [5, 1], 'item4': [3], 'item5': [9]}`
Problem
In python if I have two dictionaries, specifically Counter objects that look like so ``` c1 = Counter({'item1': 4, 'item2':2, 'item3': 5, 'item4': 3}) c2 = Counter({'item1': 6, 'item2':2, 'item3': 1, 'item5': 9}) ``` Can I combine these dictionaries so that the results is a dictionary of lists, as follows: ``` c3 = {'item1': [4,6], 'item2':[2,2], 'item3': [5,1], 'item4': [3], 'item5': [9]} ``` where each value is a list of all the values of the preceding dictionaries from the appropriate key, and where there are no matching keys between the two original dictionaries, a new kew is added that contains a one element list.