Skip to content

Commit

Permalink
Merge pull request #179 from eriknw/faster_groupby
Browse files Browse the repository at this point in the history
Faster groupby!
  • Loading branch information
mrocklin committed May 18, 2014
2 parents 189dac6 + 42dc568 commit e6a043f
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions toolz/itertoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import collections
import operator
from functools import partial
from toolz.compatibility import map, filter, filterfalse, zip, zip_longest
from toolz.compatibility import (map, filter, filterfalse, zip, zip_longest,
iteritems)


__all__ = ('remove', 'accumulate', 'groupby', 'merge_sorted', 'interleave',
Expand Down Expand Up @@ -63,12 +64,15 @@ def groupby(func, seq):
{False: [1, 3, 5, 7], True: [2, 4, 6, 8]}
See Also:
``countby``
countby
"""
d = collections.defaultdict(list)
d = collections.defaultdict(lambda: [].append)
for item in seq:
d[func(item)].append(item)
return dict(d)
d[func(item)](item)
rv = {}
for k, v in iteritems(d):
rv[k] = v.__self__
return rv


def merge_sorted(*seqs, **kwargs):
Expand Down

0 comments on commit e6a043f

Please sign in to comment.