Skip to content

Instantly share code, notes, and snippets.

@betafcc
Created February 17, 2018 21:50
Show Gist options
  • Save betafcc/3fef610a77267702e75c866d9317ff01 to your computer and use it in GitHub Desktop.
Save betafcc/3fef610a77267702e75c866d9317ff01 to your computer and use it in GitHub Desktop.
Simple python groupby
from functools import reduce
def groupby(f, it):
def reducer(acc, n):
key = f(n)
if key in acc:
acc[key].append(n)
else:
acc[key] = [n]
return acc
return reduce(reducer, it, {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment