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


Groups > comp.lang.python > #44194

Re: Nested iteration?

References <kl6a1f$k2l$1@panix2.panix.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2013-04-23 10:05 -0600
Subject Re: Nested iteration?
Newsgroups comp.lang.python
Message-ID <mailman.981.1366733175.3114.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Apr 23, 2013 at 9:40 AM, Roy Smith <roy@panix.com> wrote:
> In reviewing somebody else's code today, I found the following
> construct (eliding some details):
>
>     f = open(filename)
>     for line in f:
>         if re.search(pattern1, line):
>             outer_line = f.next()
>             for inner_line in f:
>                 if re.search(pattern2, inner_line):
>                     inner_line = f.next()
>
> Somewhat to my surprise, the code worked.  I didn't know it was legal
> to do nested iterations over the same iterable (not to mention mixing
> calls to next() with for-loops).  Is this guaranteed to work in all
> situations?

Yes, although the results will be different depending on whether the
iterable stores its iteration state on itself (like a file object) or
in the iterator (like a list).  In the latter case, you would simply
have two independent simultaneous iterations of the same object.  You
can replicate the same effect in the latter case though by getting an
iterator from the object and explicitly looping over the same
iterator, like so:

i = iter(range(10))
for x in i:
    if x % 4 == 1:
        for y in i:
            if y % 4 == 3:
                print("%d + %d = %d" % (x, y, x+y))
                break

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


Thread

Nested iteration? roy@panix.com (Roy Smith) - 2013-04-23 11:40 -0400
  Re: Nested iteration? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-23 17:05 +0100
  Re: Nested iteration? Ian Kelly <ian.g.kelly@gmail.com> - 2013-04-23 10:05 -0600
  Re: Nested iteration? Peter Otten <__peter__@web.de> - 2013-04-23 18:15 +0200
  Re: Nested iteration? Chris Angelico <rosuav@gmail.com> - 2013-04-24 02:21 +1000
  Re: Nested iteration? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-23 16:35 +0000
  Re: Nested iteration? Ian Kelly <ian.g.kelly@gmail.com> - 2013-04-23 10:30 -0600
  Re: Nested iteration? Ian Kelly <ian.g.kelly@gmail.com> - 2013-04-23 10:39 -0600
  Re: Nested iteration? Chris Angelico <rosuav@gmail.com> - 2013-04-24 02:42 +1000
    Re: Nested iteration? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-23 16:53 +0000
  Re: Nested iteration? Terry Jan Reedy <tjreedy@udel.edu> - 2013-04-23 16:49 -0400
  Re: Nested iteration? Joshua Landau <joshua.landau.ws@gmail.com> - 2013-04-23 22:14 +0100
  Re: Nested iteration? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-23 22:29 +0100
  Re: Nested iteration? Joshua Landau <joshua.landau.ws@gmail.com> - 2013-04-23 22:41 +0100
  Re: Nested iteration? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-23 23:42 +0100

csiph-web