[Python-ideas] More power in list comprehensions with the 'as' keyword

Terry Reedy tjreedy at udel.edu
Thu Aug 28 01:17:54 CEST 2008



Tarek Ziadé wrote:

>    [transform(e) as transformed for e in seq if some_test(transformed)]
> 
> In my use case I would simply have to write;:
> 
>   [l.strip() as stripped for l in text.split('\n') if stripped != '']
> 
> Which seems to me clear and concise.

-1 For me it is backward and confusing, whereas

lines = []
for l in test:
   l = l.strip()
   if l: lines.append(l)

is clear as could be.

To me, the drive to replace all for loops with list comps is 
mis-directed.  There is no end to the clauses people could propose to 
add: while, when, whatever, until, unless. mapped_to, transformed_by, 
and so on.  The result would soon by something quite different from 
Python as we know it.

tjr




More information about the Python-ideas mailing list