[Tutor] Trying to enter text from a file to a Dictionary

Kent Johnson kent37 at tds.net
Sat Jan 28 13:57:30 CET 2006


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



More information about the Tutor mailing list