About python while statement and pop()

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


On 6/11/14 10:12 PM, hito koto wrote:
> i want to change this is code:
>
> def foo(x):
>      y = []
>      while x !=[]:
>          y.append(x.pop())
>      return y
>

Consider this generator (all kinds of permutations on the idea):

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

 >>> def poplist(L):
	while True:
		yield L[::-1][:1:]
		L = L[::-1][1::][::-1]

		
 >>> pop = poplist(L1)

 >>> next(pop)
[7]
 >>> next(pop)
[6]
 >>> next(pop)
[5]
 >>> next(pop)
[4]
 >>> next(pop)
[3]
 >>> next(pop)
[2]
 >>> next(pop)
[1]
 >>> next(pop)
[]
 >>> next(pop)
[]





More information about the Python-list mailing list