Created
August 6, 2016 18:09
-
-
Save mjgpy3/2eed8ab9d8bf4524bc6b719da5788d69 to your computer and use it in GitHub Desktop.
A better Python group by
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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