[Python-ideas] Adding list.pluck()

Cenk Altı cenkalti at gmail.com
Sat Jun 2 07:25:13 CEST 2012


If the item has definded a method "__getitem__" it will be called,
else "__getattribute__" is called.

I know list comprehensions are same thing but .pluck seems easier to
read and write I think (no need to write a temporary variable in list
comprehension, and also square brackets). Just an idea...

On Sat, Jun 2, 2012 at 1:53 AM, Alexandre Zani <alexandre.zani at gmail.com> wrote:
> I must confess that I don't find "pluck" a very intuitive name for
> this functionality. For me it was evocative of what pop currently
> does. That's an N of 1 so maybe I'm just wrong on that one.
>
> More importantly, this would make the use of a list method dependent
> upon the type of the contained items. (works for dicts and nothing
> else) That would be unprecedented for list methods and potentially
> confusing. What would be the behavior if the list contains non-dicts?
>
> On Fri, Jun 1, 2012 at 2:48 PM, Cenk Altı <cenkalti at gmail.com> wrote:
>> l.pluck('name') is more readable IMO.
>>
>> On Sat, Jun 2, 2012 at 12:25 AM, Alexandre Zani
>> <alexandre.zani at gmail.com> wrote:
>>> What if it's a list of objects instead of a list of dicts? List
>>> comprehension already makes this easy:
>>>
>>> [i['name'] for i in l]
>>>
>>> I don't think this would add as much in python as it adds in javascript.
>>>
>>> On Fri, Jun 1, 2012 at 2:16 PM, Oleg Broytman <phd at phdru.name> wrote:
>>>> On Sat, Jun 02, 2012 at 12:10:03AM +0300, Cenk Alt?? <cenkalti at gmail.com> wrote:
>>>>> pluck() is a beautiful function which is in underscore.js library.
>>>>> Described as "A convenient version of what is perhaps the most common
>>>>> use-case for map: extracting a list of property values."
>>>>>
>>>>> http://documentcloud.github.com/underscore/#pluck
>>>>>
>>>>> What about it implementing for python lists? And maybe for other iterables?
>>>>
>>>>   Like operator.attrgetter?
>>>> http://docs.python.org/library/operator.html#operator.attrgetter
>>>>
>>>> Oleg.
>>>> --
>>>>     Oleg Broytman            http://phdru.name/            phd at phdru.name
>>>>           Programmers don't die, they just GOSUB without RETURN.
>>>> _______________________________________________
>>>> Python-ideas mailing list
>>>> Python-ideas at python.org
>>>> http://mail.python.org/mailman/listinfo/python-ideas
>>> _______________________________________________
>>> Python-ideas mailing list
>>> Python-ideas at python.org
>>> http://mail.python.org/mailman/listinfo/python-ideas



More information about the Python-ideas mailing list