Skip to content

Instantly share code, notes, and snippets.

@tkmru
Created December 15, 2013 20:39
Show Gist options
  • Save tkmru/7977826 to your computer and use it in GitHub Desktop.
Save tkmru/7977826 to your computer and use it in GitHub Desktop.
dict.setdefault(key, default) is if key in dict: return dict[key] else: dict[key] = default return default
def group_by(dictionary):
d = {}
for k, v in dictionary.iteritems():
d.setdefault(k, []).append(v)
return d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment