About python while statement and pop()

Mark H Harris harrismh777 at gmail.com
Thu Jun 12 12:49:12 EDT 2014


On 6/11/14 10:12 PM, hito koto wrote:

> def foo(x):
>      y = []
>      while x !=[]:
>          y.append(x.pop())
>      return y
>

Consider this generator variation:

 >>> def poplist(L):
	done = False
	while done==False:
		yield L[::-1][:1:]
		L = L[::-1][1::][::-1]
		if len(L)==0: done=True

		
 >>> L1=[1, 2, 3, 4, 5, 6, 7]

 >>> for n in poplist(L1):
	print(n)
	
[7]
[6]
[5]
[4]
[3]
[2]
[1]
 >>>




More information about the Python-list mailing list