Newbie: list comprehension troubles..

mm mattaman at gmail.com
Mon Aug 24 01:27:21 CEST 2009


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?

What I need to do is iterate over a list of graphics primitives and
call their intersect method. If the returned t value is not None then
I want to store the obj refernence and its t value in a list of tuples
for further processing. I've tried stuff like ...

        tlist = [(obj,t) for obj,t in (self.objs, obj.intersect(ray))
if (t != None)]
        tlist = [(obj,t) for obj in self.objs for t in obj.intersect
(ray) ]
        print ">>> ",len(tlist), tlist

but they don't work. Any help greatly appreciated. matt



More information about the Python-list mailing list