Pythonでunderscore.jsのpluck、groupByみたいなやつ

内包表記、おしゃれですね。

# underscore.jsのpluckみたいなやつ
users = [{"userid":1, "likes":"cats"},{"userid":2, "likes":"cats"},{"userid":3, "likes":"dogs"}]
userids = [x["userid"] for x in users]
print userids
> [1, 2, 3]

# underscore.jsのgroupByみたいなやつ
from itertools import *
users = sorted(users, key = lambda user: user['likes'])
favs = dict((k, list(u)) for k, u in groupby(users, lambda user : user['likes']))
print favs
> {'cats': [{'userid': 1, 'likes': 'cats'}, {'userid': 2, 'likes': 'cats'}], 'dogs': [{'userid': 3, 'likes': 'dogs'}]}