replacing while loop with list comprehension
问题 It is common to express for loops as list comprehensions: mylist=[] for i in range(30): mylist.append(i**2) This is equivalent to: mylist = [i**2 for i in range(30)] Is there any sort of mechanism by which this sort of iteration could be done with a while loop? mylist=[] i=0 while i<30: mylist.append(i**2) i+=1 Of course with this simple example it's easy to translate to a for loop and then to a list comprehension, but what if it isn't quite so easy? e.g. mylist = [i**2 while i=0;i<30;i++ ]