pytoolz/toolz — pull request #179 by eriknw (2014-05-10 13:36:24+00:00)
Title: Faster groupby!

Issue #178 impressed upon me just how costly attribute resolution can be. In this case, `groupby` was made faster by avoiding resolving the attribute `list.append`.

This implementation is also more memory efficient than the current version that uses a `defaultdict` that gets cast to a `dict`. While casting a defaultdict `d` to a dict as `dict(d)` is fast, it is still a fast *copy*.

Honorable mention goes to the following implementation:
```python
def groupby_alt(func, seq):
d = collections.defaultdict(lambda: [].append)
for item in seq:
d[func(item)](item)
rv = {}
for k, v in iteritems(d):
rv[k] = v.__self__
return rv
```
This alternative implementation can at times be *very* impressive. You should play with it!