Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'programmer': 0.03; 'executes': 0.09; 'resume.': 0.09; 'cc:addr:python-list': 0.11; 'jan': 0.12; 'anyway': 0.14; "'break'": 0.16; 'email addr:udel.edu>': 0.16; 'email name:<tjreedy': 0.16; 'iterator': 0.16; 'nest': 0.16; 'reedy': 0.16; 'statement.': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'received:209.85.217': 0.29; 'message-id:@mail.gmail.com': 0.30; 'terminate': 0.31; 'could': 0.34; 'received:209.85': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'surely': 0.36; 'done': 0.36; 'subject:?': 0.36; 'received:209': 0.37; 'clear': 0.37; 'skip:& 10': 0.38; 'rather': 0.38; 'break': 0.61; 'first': 0.61; 'effectively': 0.66; 'clearer': 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=oh35bbnIK7GcQjN7hDH12AVtKXn271MGhAJrmWlMRDg=; b=xtV2m2natbbLT+VLpVs13zbcD91vR5OPws/wgb3PiUN5Xho4fiTml6vmDN2PspyecA fAt0R+xGMVcqPHZA4AhSaYiw87cL3TK8KsV75mntcWH7lwsixYanaiKNYIDL6mTcNOIq zeKWx5dxzT5t30LeMIAJwWvTd3B1Tc7DwOCWqgM70alWUjpFg2yYb84FzVw7hBbi2UVM KAhF/sOBBNmOxnF83VvkPnqdPhC/Af9q/vfBmeYCNqgrfflCEe7l+o0N9y2fQ1OupCm1 Vj3PU6IiCu7cgkLyqQYke9zKdnY0ZquFA5zSadpNs5Yjgwq+KIKa1+PDJEeblatIX/Fc cdjg== X-Received: by 10.112.149.200 with SMTP id uc8mr3819701lbb.9.1366751689171; Tue, 23 Apr 2013 14:14:49 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Joshua Landau Date: Tue, 23 Apr 2013 22:14:09 +0100 Subject: Re: Nested iteration? To: Terry Jan Reedy Content-Type: multipart/alternative; boundary=047d7b342d74edea2704db0dacc4 Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 73 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366751696 news.xs4all.nl 2304 [2001:888:2000:d::a6]:44621 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44215 --047d7b342d74edea2704db0dacc4 Content-Type: text/plain; charset=ISO-8859-1 On 23 April 2013 21:49, Terry Jan Reedy wrote: > ri= iter(range(3)) > for i in ri: > for j in ri: > print(i,j) > # this is somewhat deceptive as the outer loop executes just once > 0 1 > 0 2 > > I personally would add a 'break' after 'outer_line = next(f)', since the > first loop is effectively done anyway at that point, and dedent the second > for statement. I find to following clearer > > ri= iter(range(3)) > for i in ri: > break > for j in ri: > print(i,j) > # this makes it clear that the first loop executes just once > 0 1 > 0 2 > > I would only nest if the inner loop could terminate without exhausting the > iterator and I wanted the outer loop to then resume. > Surely a normal programmer would think "next(ri, None)" rather than a loop that just breaks. --047d7b342d74edea2704db0dacc4 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On 23 April 2013 21:49, Terry Jan Reedy = <tjreedy@udel.edu<= /a>> wrote:
ri=3D iter(range(3))<= /span>
for i in ri:
=A0 =A0 for j in ri:
=A0 =A0 =A0 =A0 print(i,j)
# this is somewhat deceptive as the outer loop executes just once
0 1
0 2

I personally would add a 'break' after 'outer_line =3D next(f)&= #39;, since the first loop is effectively done anyway at that point, and de= dent the second for statement. I find to following clearer

ri=3D iter(range(3))
for i in ri:
=A0 =A0 break
for j in ri:
=A0 =A0 print(i,j)
# this makes it clear that the first loop executes just once
0 1
0 2

I would only nest if the inner loop could terminate without exhausting the = iterator and I wanted the outer loop to then resume.
<= br>
Surely a normal programmer would think "next(ri, N= one)" rather than a loop that just breaks.
--047d7b342d74edea2704db0dacc4--