How do I convert a list of dictionaries to a dictionary of lists in Python?

dictionary, python

Solution

import collections

result = collections.defaultdict(list)

for d in dictionaries:
    for k, v in d.items():
        result[k].append(v)

Problem

It may be a classical question in Python, but I haven't found the answer yet. I have a list of dictionaries, these dictionaries have similar keys. It looks like this: ``` [{0: myech.MatchingResponse at 0x10d6f7fd0, 3: myech.MatchingResponse at 0x10d9886d0, 6: myech.MatchingResponse at 0x10d6f7d90, 9: myech.MatchingResponse at 0x10d988ad0}, {0: myech.MatchingResponse at 0x10d6f7b10, 3: myech.MatchingResponse at 0x10d6f7f90>}] ``` I would like to get a new dictionary with [0,3,6,9] as keys, and lists of " myech.MatchingResponse" as values. Of course I can do this using a simple loop but I was wondering if there is a more efficient solution.

Original source

Related problems