'while' in list comprehension?

Emile van Sebille emile at fenx.com
Wed Oct 22 15:53:38 EDT 2003


Paul Rubin...
> > >>> bar = range(-5,5)
> > >>> bar
> > [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
> > >>> foo = [i for i in bar if i>0]
> > >>> foo
> > [1, 2, 3, 4]
>
> Not the same.  Try bar = [1, 2, -3, 4, 5]

How about...

>>> bar = [1, 2, -3, 4, 5]
>>> go = 1
>>> [x for x in bar for go in [go and x>0] if go ]
[1, 2]
>>>

<ducking... but expecting this to still be legal despite new scoping under
consideration...>

Emile van Sebille
emile at fenx.com






More information about the Python-list mailing list