[Tutor] Getting list of attributes from list of objects
Douglas Philips
dgou at mac.com
Thu Sep 10 18:20:50 CEST 2009
On or about 2009 Sep 10, at 11:36 AM, Serdar Tumgoren indited:
> I think a list comprehension might be the most compact way to
> accomplish what you're after:
>
> objcts = [a, b, c]
>
> titles = [obj.title for obj in objcts]
That probably is the best form.
For the sake of showing other options:
from operator import attrgetter
#...
titles = map(attrgetter("title"), objcts)
# I love placeholder for this kind of thing.
from placeholder import __
#...
titles = map(__.title, objcts)
--Doug
P.S. placeholder can be found at: http://pypi.python.org/pypi/placeholder
P.P.S. I have a version that composes, so you can say: map((__ + 3) *
2, my_numbers) -- http://bitbucket.org/dgou/placeholder2/
More information about the Tutor
mailing list