'while' in list comprehension?

Batista, Facundo FBatista at uniFON.com.ar
Wed Oct 22 14:11:34 EDT 2003


jsaul wrote:

#- 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 ]

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]
>>> 

.	Facundo





More information about the Python-list mailing list