Iterator, modify data in loop body

Thomas Rachel nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de
Tue Sep 16 04:49:22 EDT 2014


Am 13.09.2014 09:22 schrieb Chris Angelico:

> In that case, don't iterate over the list at all. Do something like this:
>
> while lst:
>      element = lst.pop(0)
>      # work with element
>      lst.append(new_element)

And if you don't like that, define a

def iter_pop(lst):
     while lst:
         yield lst.pop(0)

and you can do

     for element in iter_pop(lst):
         # work with element
         lst.append(new_element)


Thomas



More information about the Python-list mailing list