Alan Gauld wrote: > Hi Bob, > > >>"list comprehension" (once understood) is often easier to read and more >>efficient than the for loop. > > > They are often more efficient but I don't know if I'd ever claim they were > easier to read than an explicit for loop. Perhaps the most trivial cases > like > > z = [x*2 for x in L] > > and even then I'm not sure that is easier to read than > > z = [] > for x in L: z.append(x*2)
I find simple list comps far easier to read and write than the equivalent for loop, and they fit the way I think about problems - I will think, "I need a list of the squares of everything in L." This matches exactly the order of elements in a list comp. Personally I avoid using list comps purely for the side effects, to me that breaks the conceptual "I need a list...". I admit that some of the most unreadable one-liners on comp.lang.python use list comps in creative ways...I write out the loop rather than going to contortions to make an expression I can use in a list comp. I don't see the point in twistng the code to fit it into a list comp. Kent _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor