Skip to content

Instantly share code, notes, and snippets.

@cyclecycle
Last active August 11, 2020 10:39
Show Gist options
  • Save cyclecycle/6fac4b35c5467d397ec242a3f4bdf4db to your computer and use it in GitHub Desktop.
Save cyclecycle/6fac4b35c5467d397ec242a3f4bdf4db to your computer and use it in GitHub Desktop.
from itertools import groupby
def groupby_(obj, key=None, to_lists=True):
sorted_ = sorted(obj, key=key)
grouped = groupby(sorted_, key)
if to_lists:
grouped = {k: list(g) for k, g in grouped}
return grouped
unique_theme_key = lambda mapping: (mapping['theme']['name']), mapping['theme']['group'])
grouped = groupby_(sent_theme_mappings, key=unique_theme_key)
for theme_key, group in grouped.items():
# theme_key: (name, group)
# group: list of mappings corresponding to theme
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment