[Tutor] Getting list of attributes from list of objects

vince spicer vinces1979 at gmail.com
Thu Sep 10 17:31:11 CEST 2009


On Thu, Sep 10, 2009 at 8:51 AM, Oleg Oltar <oltarasenko at gmail.com> wrote:

> Hi!
>
> I have the following list
>
> l= [ a, b, c]
>
> Where a,b,c are objects created from one class, e.g. each has title
> attribute.
> What I want to have is a list of titles, e.g. [a.title, b.title, c.title]
>
> Is there a quick way to do it? Or I should use loops (looping through each
> element, and generating list)
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
you can use list comprehension, which is a type of loop, but looks so pretty
:)

titles = [x.title for x in l]

Vince
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090910/ebda82de/attachment.htm>


More information about the Tutor mailing list