Skip to content

Instantly share code, notes, and snippets.

@WayneSan
Last active August 29, 2015 14:24
Show Gist options
  • Save WayneSan/3b1b4bb593a7c259263e to your computer and use it in GitHub Desktop.
Save WayneSan/3b1b4bb593a7c259263e to your computer and use it in GitHub Desktop.
# users = queryset.values_list('group', 'username').distinct()
users = [('admin', 'root'), ('group1', 'andy'), ('group1', 'tim')]
grouped_users = reduce(lambda d, (k, v): dict(d, **{k: d.get(k, [])+[v]}), users, {})
print grouped_users # {'admin': ['root'], 'group1': ['andy', 'tim']}
choices = map(lambda (k, v): (k, [('%s_%s' % (k, x), x) for x in v]), grouped_users.items())
print choices # [('admin', [('admin_root', 'root')]), ('group1', [('group1_andy', 'andy'), ('group1_tim', 'tim')])]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment