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


Groups > comp.lang.python > #77831

Iterator, modify data in loop body

From Michael Welle <mwe012008@gmx.net>
Newsgroups comp.lang.python
Subject Iterator, modify data in loop body
Date 2014-09-13 08:09 +0200
Organization MB-NET.NET for Open-News-Network e.V.
Message-ID <uk3debxpcg.ln2@news.c0t0d0s0.de> (permalink)

Show all headers | View raw


Hello,

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.

Ex.:

foo = [1,2,3,4]
it = iter(foo)

for e in it:
    if e % 2 == 0:
        x.append(e)


Regards
hmw

-- 
biff4emacsen - A biff-like tool for (X)Emacs
http://www.c0t0d0s0.de/biff4emacsen/biff4emacsen.html
Flood - Your friendly network packet generator
http://www.c0t0d0s0.de/flood/flood.html

Back to comp.lang.python | Previous | NextNext 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