Newbie: list comprehension troubles..

Chris Rebert clp2 at rebertia.com
Sun Aug 23 19:36:02 EDT 2009


On Sun, Aug 23, 2009 at 4:27 PM, mm<mattaman at gmail.com> wrote:
> Hi, I'm trying to replace this...
>
>        # this works but there must be a more pythonic way, right?
>        tlist = []
>        for obj in self.objs:
>            t = obj.intersect(ray)
>            if (t != None):
>                tlist.append((obj,t))
>
> with a list comprehension- can it be done?

Yes:
tlist = [pair for pair in ((obj, obj.intersect(ray)) for obj in
self.objs) if pair[1] is not None]

Should it be done? Probably not. It's less readable and less efficient.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list