Skip to content

Instantly share code, notes, and snippets.

@mjgpy3
Created August 6, 2016 18:09
Show Gist options
  • Save mjgpy3/2eed8ab9d8bf4524bc6b719da5788d69 to your computer and use it in GitHub Desktop.
Save mjgpy3/2eed8ab9d8bf4524bc6b719da5788d69 to your computer and use it in GitHub Desktop.
A better Python group by
def group_by(values, grouping_factor):
results = {}
for value in values:
key = grouping_factor(value)
if key in results:
results[key].append(value)
else:
results[key] = [value]
return zip(results.keys(), results.values())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment