Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77837

Re: Iterator, modify data in loop body

From Peter Otten <__peter__@web.de>
Subject Re: Iterator, modify data in loop body
Date 2014-09-13 09:34 +0200
Organization None
References <uk3debxpcg.ln2@news.c0t0d0s0.de>
Newsgroups comp.lang.python
Message-ID <mailman.13993.1410593701.18130.python-list@python.org> (permalink)

Show all headers | View raw


Michael Welle wrote:

> I want to create an iterator it=iter(list) and control a for-loop with
> it. Is it save to append elements to the list in the body of the
> for-loop or is the behaviour undefined then? PEP234 notes that once the
> iterator has signaled exhaustion, subsequent calls of next() should not
> change that state. That suggests that it is possible to modify the list
> during the iterator's livetime.

It's possible, but usually not a good idea. Especially inserting or deleting 
elements before the current position of the iterator (you can think of it as 
an index into the list) gives results that are usually unexpected:

>>> items = [1, "remove me", 2, "remove me", "remove me", 3, 4]
>>> for item in items:
...     if item == "remove me":
...         items.remove(item)
... 
>>> items
[1, 2, 'remove me', 3, 4]

Pro tip: don't do it even when it's possible.

> Ex.:
> 
> foo = [1,2,3,4]
> it = iter(foo)
>
> for e in it:
>     if e % 2 == 0:
>         x.append(e)

I don't see how the example is related to the question. Did you mean

   foo.append(e)

? With that modification the loop would run "forever" because you keep 
appending items that satisfy the condition e % 2 == 0.

> it = iter(foo)

Normally you would just iterate over foo; the only use-case where you'd 
create an iterator explicitly is when you want to skip some items:

it = iter(items)
for e in it:
    if skip_next(e):
        next(it)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Iterator, modify data in loop body Michael Welle <mwe012008@gmx.net> - 2014-09-13 08:09 +0200
  Re: Iterator, modify data in loop body Chris Angelico <rosuav@gmail.com> - 2014-09-13 16:22 +1000
    Re: Iterator, modify data in loop body Michael Welle <mwe012008@gmx.net> - 2014-09-13 09:01 +0200
      Re: Iterator, modify data in loop body Chris Angelico <rosuav@gmail.com> - 2014-09-13 17:22 +1000
        Re: Iterator, modify data in loop body Michael Welle <mwe012008@gmx.net> - 2014-09-13 09:39 +0200
          Re: Iterator, modify data in loop body Ian Kelly <ian.g.kelly@gmail.com> - 2014-09-13 09:27 -0600
            Re: Iterator, modify data in loop body Michael Welle <mwe012008@gmx.net> - 2014-09-13 18:58 +0200
          Re: Iterator, modify data in loop body Chris Angelico <rosuav@gmail.com> - 2014-09-14 01:32 +1000
        Re: Iterator, modify data in loop body Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2014-09-16 10:49 +0200
          Re: Iterator, modify data in loop body Chris Angelico <rosuav@gmail.com> - 2014-09-17 01:19 +1000
  Re: Iterator, modify data in loop body Peter Otten <__peter__@web.de> - 2014-09-13 09:34 +0200
    Re: Iterator, modify data in loop body Michael Welle <mwe012008@gmx.net> - 2014-09-13 09:55 +0200

csiph-web