Using a generator expression or list comprehension to do this is so
easy and readable I don't see why we'd want something new in Python.
Mike
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?
> l.pluck('name') is more readable IMO.
Only because you already associate "pluck" with that meaning.
As others said, "pluck" to me implies something like "pop". The list
comprehension spelling doesn't suffer from this problem, and provides
a lot more flexibility. If you don't like list comprehensions, use map
and the operator module.
Even if it is more readable, it's more semantic load. It's another
container operator (and one that's only useful in the special case of
a list of maps) people have to learn. Since it saves 0 lines of code
over either of existing mechanisms, the extra load comes for no
advantage.
-1
<mike
--
Mike Meyer <m...@mired.org> http://www.mired.org/
Independent Software developer/SCM consultant, email for more information.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
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
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...
> If the item has definded a method "__getitem__" it will be called,
> else "__getattribute__" is called.
>
That is complète insanity and goes against pretty much all existing python code. Some types make keys available as both items and attributes but I do not know of any operation which does so.
> 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...
Not a useful one, if you dislike the iteration variable of comprehensions you may use 'map' with itemgetter or attrgetter. Not to mention they are more flexible than pluck (they can extract multiple items or attributes, and attrgetter supports "deep" dotted paths)
>>> stooges=[{'name': 'moe', 'age': 40}, {'name': 'larry', 'age': 50}, {'name': 'curly', 'age': 60}]
>>> [guy['name'] for guy in stooges]
['moe', 'larry', 'curly']
>>> from operator import itemgetter
>>> map(itemgetter('name'),stooges)
['moe', 'larry', 'curly']
Also I'm used to such functions being called "collect" (Ruby) or "map" (Python, jQuery) and
accepting a function/block as an argument. In Ruby-on-Rails it can be &:name as a shorthand for
{|item| item[:name]}, which is equivalent to itemgetter('name') in Python. So if you insist of
making it shorter (but less readable) you could do:
>>> from operator import itemgetter as G
>>> map(G('name'),stooges)
['moe', 'larry', 'curly']
This is a case where a simple list comprehension or generator
expression would be a lot easier to understand than remembering what a
rarely used method name does. Also, it couples two distinct
interfaces, iterables and mappings, in a way that is generally frowned
upon in Python.
> _______________________________________________
> Python-ideas mailing list
> Python...@python.org
> http://mail.python.org/mailman/listinfo/python-ideas
--
Read my blog! I depend on your acceptance of my opinion! I am interesting!
http://techblog.ironfroggy.com/
Follow me if you're into that sort of thing: http://www.twitter.com/ironfroggy