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


Groups > comp.lang.python > #44193

Re: Nested iteration?

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.027
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'nested': 0.07; 'worked.': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'thread': 0.14; "wouldn't": 0.14; 'posted': 0.15; 'iterable': 0.16; 'iterator': 0.16; 'reason.': 0.16; 'roy': 0.16; 'wrote:': 0.18; '(not': 0.18; 'otherwise,': 0.22; 'cc:addr:python.org': 0.22; 'fine': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'mention': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'catching': 0.31; 'though.': 0.31; 'another': 0.32; '(i.e.': 0.33; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'received:google.com': 0.35; "didn't": 0.36; 'subject:?': 0.36; 'received:209': 0.37; 'somebody': 0.38; 'rather': 0.38; 'bad': 0.39; 'new': 0.61; 'today,': 0.61; 'skip:n 10': 0.64; 'smith': 0.68; 'legal': 0.71; 'guaranteed': 0.75; "else's": 0.84; 'oscar': 0.84; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=AsJIn63HDg83fYoO30qg6qaBWiMj6GxT60tz4f8LoZg=; b=eWZZoQVkzEcvehaqApjVvVWkSLkipZmD8XebSL2cCorYtFAW4Cytoqn7acSfUfAB5/ S802JyS6//RRjgC+fLVH3VjrsFwF2GhUHFJnxtSL2GCma43IqVE9CnDKo/6DVQkbUcMs X7nYAGZj3W1WU7oIJo/acOSCQJqDadDcixs9DkrNwa6BpUmRoJMGdbP4GYB35EIzEtMk w4Q7ydnc1sKwuKHWlLe4tTTrALj4ZO/p/Yf/PSS+MJwKd7fZ10t6fhRYG+/ssvj26qvC RXRougMMeVEIVR0hg/NtkjPBijF6zgqwKC7WiFOTTzZAHbLilRbLISvNLotcUYd2H8eS 7tzg==
X-Received by 10.58.220.129 with SMTP id pw1mr23056526vec.32.1366733134908; Tue, 23 Apr 2013 09:05:34 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <kl6a1f$k2l$1@panix2.panix.com>
References <kl6a1f$k2l$1@panix2.panix.com>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Tue, 23 Apr 2013 17:05:13 +0100
Subject Re: Nested iteration?
To Roy Smith <roy@panix.com>
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.980.1366733143.3114.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1366733143 news.xs4all.nl 2168 [2001:888:2000:d::a6]:55274
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:44193

Show key headers only | View raw


On 23 April 2013 16:40, 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?

For Python 3 you'd need next(f) instead of f.next(). Otherwise, yes,
this works just fine with any non-restarting iterator (i.e. so that
__iter__ just returns self rather than a new iterator).

I recently posted in another thread about why it's a bad idea to call
next() without catching StopIteration though. I wouldn't accept the
code above for that reason.


Oscar

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