I started reading about underscore.js today, it is a library for javascript that adds some functional programming goodies I’m used to using in Python. One pretty cool shorthand method is pluck.
Indeed in Python I often need to pluck out some specific attribute, and end up doing this:
users = [{
"name" : "Bemmu",
"uid" : "297200003"
},
{
"name" : "Zuck",
"uid" : "4"
}]
uids = map(lambda x:x["uid"], users)
If the underscore shorthand is somewhere in Python, this would be possible:
uids = pluck(users, "uid")
It’s of course trivial to add, but is that in Python somewhere already?
Just use a list comprehension in whatever function is consuming
uids:instead of
do
If you just want
uidsto iterate over, you don’t need to make a list — use a generator instead. (Replace[]with().)For example: