'while' in list comprehension?

Paul Rubin http
Wed Oct 22 15:14:15 EDT 2003


"Batista, Facundo" <FBatista at uniFON.com.ar> writes:
> #-     foo = [ i for i in bar while len(i)>0 ]
> 
> You have the 'if' statement:
> 
> >>> 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]




More information about the Python-list mailing list