Unpack dictionary that contains a list of dictionaries and insert in columns
Question: With the data below, I'm trying to unfold a dictionary that contains a list of
dictionaries, and then group each key with the corresponding values of the
other dictionaries together. For example:

    
    
    result = {
        'themes' : [{
                'a' : 'orange',
                'b' : 6,
                'c' : 'neutral',
                'd' : 6,
                'e' : 0.24
            }, {
                'a' : 'banana',
                'b' : 6,
                'c' : 'neutral',
                'd' : 6,
                'e' : 0.16
            }, {
                'a' : 'phone',
                'b' : 5,
                'c' : 'neutral',
                'd' : 5,
                'e' : 0.02
            }
        ]
    }
    

...should become something along these lines:

    
    
    themes={'a' : ['orange','banana', 'phone']}
    
    count={'b' : [6,6,5]}
    
    s_score={'c' : [neutral, neutral, neutral]}
    

...and so on.

I've looked [here](http://stackoverflow.com/questions/16485247/dictionary-
that-contains-list), [here](http://stackoverflow.com/questions/3421906/how-to-
merge-lists-of-dictionaries), and
[here](http://stackoverflow.com/questions/38987/how-can-i-merge-two-python-
dictionaries-in-a-single-expression) among other places, but couldn't find
something close enough to what I want to do.
[This](http://stackoverflow.com/questions/26972204/combine-dictionary-entries-
by-common-elements) came pretty close, but it's checking for at least one or
more common values, whereas mine should group common keys. I know I can
separate the outer key from the values like this:

    
    
    >>>(k, v), = result.items()
    >>>k
    >>>'themes'
    >>>v
    >>>[{
            'a' : 'orange',
            'b :6,
            'c' : 'neutral',
            'd' : 6,
            'e' : 0.24
        }, {
            'a' : 'banana',
            'b' : 6,
            'c' : 'neutral',
            'd' : 6,
            'e' : 0.16
        }, {
            'a' : 'phone',
            'b' : 5,
            'c' : 'neutral',
            'd' : 5,
            'e' : 0.02
        }
    ]
    

but how do I get the v list of dictionaries to the way I described? Do I have
to convert them to sets first?

To make my intention clear, my ultimate goal is iterate through the list of
values of the keys that I want to keep, so I can enter them into their
respective columns in my fairly basic flask-sqlalchemy SQLite database. So in
the end I'll be able to query and get them displayed as html:

    
    
    +-----------------+----------+----------+-------+
    |       a         |    b     |    c     |   d   |
    +-----------------+----------+----------+-------+
    |     orange      |   2.4    | neutral  |   6   |
    |     banana      |   1.6    | neutral  |   6   |
    +-----------------+----------+----------+-------+
    


Answer: You should first flatMap all your values in the list of tuples (`[('a',
'orange'), ('c', 'neutral'), ('b', '6')`..]) and then groupBy first element. I
would do it this way:

    
    
    import itertools
    
    pairs =  itertools.chain.from_iterable([d.items() for d in result["themes"]])
    result = {}
    for key, elem in pairs:
        result.setdefault(key, []).append(elem)
    print result 
    

