[Python-ideas] Adding list.pluck()
Jerry Hill
malaclypse2 at gmail.com
Sat Jun 2 01:16:37 CEST 2012
On Fri, Jun 1, 2012 at 5:48 PM, Cenk Altı <cenkalti at gmail.com> wrote:
> l.pluck('name') is more readable IMO.
That's not how the library you linked to works, as far as I can tell.
Based on the sample usage at
http://documentcloud.github.com/underscore/#pluck, pluck is a
function taking an iterable of dictionaries and the key, so I think
the python equivalent is:
def pluck(iterable, key):
return [item[key] for item in iterable]
stooges = [{'name' : 'moe', 'age' : 40},
{'name' : 'larry', 'age' : 50},
{'name' : 'curly', 'age' : 60}]
print (pluck(stooges, 'name'))
>>>
['moe', 'larry', 'curly']
--
Jerry
More information about the Python-ideas
mailing list