Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping items by a key?

I feel like Python ought to have a built-in to do this. Take a list of items and turn them into a dictionary mapping keys to a list of items with that key in common.

It's easy enough to do:

# using defaultdict
lookup = collections.defaultdict(list)
for item in items:
    lookup[key(item)].append(item)

# or, using plain dict
lookup = {}
for item in items:
    lookup.setdefault(key(item), []).append(item)

But this is frequent enough of a use case that a built-in function would be nice. I could implement it myself, as such:

def grouped(iterable, key):
    result = {}
    for item in iterable:
        result.setdefault(key(item), []).append(item)
    return result

lookup = grouped(items, key)

This is different than itertools.groupby in a few important ways. To get the same result from groupby, you'd have to do this, which is a little ugly:

lookup = dict((k, list(v)) for k, v in groupby(sorted(items, key=key), key))

Some examples:

>>> items = range(10)
>>> grouped(items, lambda x: x % 2)
{0: [0, 2, 4, 6, 8], 1: [1, 3, 5, 7, 9]}

>>> items = 'hello stack overflow how are you'.split()
>>> grouped(items, len)
{8: ['overflow'], 3: ['how', 'are', 'you'], 5: ['hello', 'stack']}

Is there a better way?

like image 841
FogleBird Avatar asked Oct 04 '22 15:10

FogleBird


2 Answers

I also posted this question to comp.lang.python, and the consensus seems to be that this isn't actually common enough to warrant a built-in function. So, using the obvious approaches are best. They work and they're readable.

# using defaultdict
lookup = collections.defaultdict(list)
for item in items:
    lookup[key(item)].append(item)

# or, using plain dict
lookup = {}
for item in items:
    lookup.setdefault(key(item), []).append(item)

I was going to delete my question, but I might as well leave this here in case anyone stumbles across it looking for information.

like image 160
FogleBird Avatar answered Oct 10 '22 01:10

FogleBird


If you wanted something with roughly the same API as groupby, you could use:

def groupby2(iterable, keyfunc):
    lookup = collections.defaultdict(list)
    for item in iterable:
        lookup[keyfunc(item)].append(item)
    return lookup.iteritems()

So that's the same as your example above, but made into a function returning the iteritems of the lookup table you've built.

like image 20
tobych Avatar answered Oct 10 '22 03:10

tobych