Newbie: list comprehension troubles..

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Mon Aug 24 04:54:31 EDT 2009


mm a écrit :
> 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):

hint 1 : this is Python, you don't need parens around conditionals.
hint 2 : None is a singleton, so the usual idiom is to test for identity 
('is') instead of equality ('==')

IOW, make this line:

               if t is not None:

>                 tlist.append((obj,t))
> 
> with a list comprehension- can it be done?

See other answers here. As far as I'm concerned, while being a great fan 
of list comps etc, I'd stick with the plain old for loop here - much 
more readable if nothing else.




More information about the Python-list mailing list