Skip to content

Instantly share code, notes, and snippets.

@TaurusOlson
Forked from mrocklin/groupby.py
Created March 1, 2014 00:49
Show Gist options
  • Save TaurusOlson/9282970 to your computer and use it in GitHub Desktop.
Save TaurusOlson/9282970 to your computer and use it in GitHub Desktop.
# Passes https://gist.github.com/mrocklin/5722155
def groupby(f, coll):
""" Group elements in collection by ``f`` """
d = dict()
for item in coll:
key = f(item)
if key not in d:
d[key] = []
d[key].append(item)
return d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment