Pythonic way to group items in a list
Question: Consider a list of dicts:

    
    
    items = [
        {'a': 1, 'b': 9, 'c': 8},
        {'a': 1, 'b': 5, 'c': 4},
        {'a': 2, 'b': 3, 'c': 1},
        {'a': 2, 'b': 7, 'c': 9},
        {'a': 3, 'b': 8, 'c': 2}
    ]
    

Is there a pythonic way to extract and group these items by their `a` field,
such that:

    
    
    result = {
        1 : [{'b': 9, 'c': 8}, {'b': 5, 'c': 4}]
        2 : [{'b': 3, 'c': 1}, {'b': 7, 'c': 9}]
        3 : [{'b': 8, 'c': 2}]
    }
    

References to any similar Pythonic constructs are appreciated.


Answer: Use
[`itertools.groupby`](https://docs.python.org/2/library/itertools.html#itertools.groupby):

    
    
    >>> from itertools import groupby
    >>> from operator import itemgetter
    >>> {k: list(g) for k, g in groupby(items, itemgetter('a'))}
    {1: [{'a': 1, 'c': 8, 'b': 9},
         {'a': 1, 'c': 4, 'b': 5}],
     2: [{'a': 2, 'c': 1, 'b': 3},
         {'a': 2, 'c': 9, 'b': 7}],
     3: [{'a': 3, 'c': 2, 'b': 8}]}
    

If item are not in sorted order then you can either sort them and then use
`groupby` or you can use `collections.OrderedDict`(if order matters) or
`collections.defaultdict` to do it in O(N) time:

    
    
    >>> from collections import OrderedDict
    >>> d = OrderedDict()
    >>> for item in items:
    ...     d.setdefault(item['a'], []).append(item)
    ...     
    >>> dict(d.items())
    {1: [{'a': 1, 'c': 8, 'b': 9},
         {'a': 1, 'c': 4, 'b': 5}],
     2: [{'a': 2, 'c': 1, 'b': 3},
         {'a': 2, 'c': 9, 'b': 7}],
     3: [{'a': 3, 'c': 2, 'b': 8}]}
    

**Update:**

I see that you only want the those keys to be returned that we didn't use for
grouping, for that you'll need to do something like this:

    
    
    >>> group_keys = {'a'}
    >>> {k:[{k:d[k] for k in d.viewkeys() - group_keys} for d in g]
                                       for k, g in groupby(items, itemgetter(*group_keys))}
    {1: [{'c': 8, 'b': 9},
         {'c': 4, 'b': 5}],
     2: [{'c': 1, 'b': 3},
         {'c': 9, 'b': 7}],
     3: [{'c': 2, 'b': 8}]}
    

