'while' in list comprehension?
Terry Reedy
tjreedy at udel.edu
Wed Oct 22 22:57:42 EDT 2003
"jsaul" <jsaul at gmx.de> wrote in message
news:20031022175924.GA10716 at jsaul.de...
> Hi there,
>
> wouldn't it be useful to have a 'while' conditional in addition to
> 'if' in list comprehensions?
>
> foo = []
> for i in bar:
> if len(i) == 0:
> break
> foo.append(i)
>
> would then turn into
>
> foo = [ i for i in bar while len(i)>0 ]
while is simply not same as if: break!
if executes once for each value of i, while indefinitely.
If you translate back by current rule, which will not change, you get:
foo = []
for i in bar:
while len(i) >0:
foo.append(i)
Terry J. Reedy
More information about the Python-list
mailing list