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


Groups > comp.lang.python > #92582

Re: zip as iterator and bad/good practices

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <jimages123@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'modify': 0.04; '[1,': 0.09; 'debugger': 0.09; 'loop.': 0.09; 'recommends': 0.09; 'bug': 0.10; 'skip:# 20': 0.13; '*never*': 0.16; 'element.': 0.16; 'iterating': 0.16; 'looping.': 0.16; 'wrote:': 0.16; 'element': 0.18; 'pointer': 0.18; 'to:name:python-list@python.org': 0.20; 'see:': 0.22; 'header:In-Reply-To:1': 0.24; 'script': 0.25; 'subject:/': 0.29; 'appending': 0.29; 'pointer.': 0.29; 'launch': 0.29; 'code': 0.31; 'received:google.com': 0.34; 'message- id:@gmail.com': 0.35; 'to:addr:python-list': 0.35; 'next': 0.35; 'something': 0.35; 'list': 0.35; 'but': 0.36; 'there': 0.36; 'subject:: ': 0.37; 'charset:us-ascii': 0.37; '12,': 0.37; 'pm,': 0.39; 'test': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'header:Message-Id:1': 0.62; 'subject:good': 0.84; 'try.': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:references:from:content-type:in-reply-to:message-id:date:to :content-transfer-encoding:mime-version; bh=UHSLXphSt4xPGri7mYgnqTEbyCO6cFRLhKclSYN+y5o=; b=XqZ7wzjlcZBgzUPOG55vzA264pt4Q0aTTyG1lOha2KcrXTsjdY40fHDGxZ140W57M0 31Ahxfpl0nK0WsTG1Vw58x6+Bz9fogm8HoIhRxs68nk7ImIJ5i2YzLkBB+vjHhFOE9Ob I+ILkY8fWFFNqJM4PUCbXb/fMI0FKjixQiYgEGDr1hBy87e5yZAW1I1OildhaBw1VL4X D3EUKUIz2OmFoqPY/tQsXLp5najVNONzXh1DPaBJ5xF1ONLudc22iKZXk7lk/Eq0xQs8 xyUcgWc1kVPU1Kz39HhYYM/z/rnOIq+pA2Y/BRlG9js1oHZ39uNth/sLMoX2JAS1wokx bXDQ==
X-Received by 10.70.138.8 with SMTP id qm8mr28823740pdb.96.1434174223375; Fri, 12 Jun 2015 22:43:43 -0700 (PDT)
Subject Re: zip as iterator and bad/good practices
References <mles66$sk2$1@speranza.aioe.org>
From jimages <jimages123@gmail.com>
Content-Type text/plain; charset=us-ascii
X-Mailer iPhone Mail (11D257)
In-Reply-To <mles66$sk2$1@speranza.aioe.org>
Date Sat, 13 Jun 2015 13:32:59 +0800
To "python-list@python.org" <python-list@python.org>
Content-Transfer-Encoding quoted-printable
Mime-Version 1.0 (1.0)
X-Mailman-Approved-At Sat, 13 Jun 2015 07:57:57 +0200
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.452.1434175078.13271.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1434175078 news.xs4all.nl 2862 [2001:888:2000:d::a6]:44363
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:92582

Show key headers only | View raw



> On Jun 12, 2015, at 11:00 PM, Fabien <fabien.maussion@gmail.com> wrote:
> but that awful bug made me wonder: is it a bad practice to interactively modify the list you are iterating over?
Yes.
I am a newbie. I also have been confused when I read the tutorial. It recommends make a copy before looping. Then I try.
#--------------------------
Test = [1, 2]
For i in Test:
    Test.append(i)
#--------------------------
But when i execute. The script does not end. I know there must something wrong. So I launch debugger and deserve the list after each loop.
And I see:
Loop 1: [ 1, 2, 1]
Loop 2: [ 1, 2, 1, 2]
Loop 3: [ 1, 2, 1, 2, 1]
Loop 4: [ 1, 2, 1, 2, 1, 2]
......
So you can see that loop will *never* end.
So I think you regard the 'i' as a pointer. After execute one loop the pointer repoints to next element , but at the same time you are appending element. So pointer will *never* repoints to the last element.
How to solve?
Change code to this
#--------------------------
Test = [1, 2]
For i in Test[:] :
    Test.append(i)
#--------------------------

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


Thread

zip as iterator and bad/good practices Fabien <fabien.maussion@gmail.com> - 2015-06-12 17:00 +0200
  Re: zip as iterator and bad/good practices Fabien <fabien.maussion@gmail.com> - 2015-06-12 17:05 +0200
  Re: zip as iterator and bad/good practices Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-12 09:26 -0600
    Re: zip as iterator and bad/good practices Fabien <fabien.maussion@gmail.com> - 2015-06-12 17:34 +0200
    Re: zip as iterator and bad/good practices Fabien <fabien.maussion@gmail.com> - 2015-06-12 17:59 +0200
  Re: zip as iterator and bad/good practices Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-06-12 17:22 +0100
  Re: zip as iterator and bad/good practices Laura Creighton <lac@openend.se> - 2015-06-12 22:34 +0200
  Re: zip as iterator and bad/good practices Terry Reedy <tjreedy@udel.edu> - 2015-06-12 19:27 -0400
  Re: zip as iterator and bad/good practices Terry Reedy <tjreedy@udel.edu> - 2015-06-12 19:43 -0400
    Re: zip as iterator and bad/good practices sohcahtoa82@gmail.com - 2015-06-12 17:02 -0700
      Re: zip as iterator and bad/good practices Chris Angelico <rosuav@gmail.com> - 2015-06-13 10:26 +1000
        Re: zip as iterator and bad/good practices sohcahtoa82@gmail.com - 2015-06-12 17:39 -0700
  Re: zip as iterator and bad/good practices jimages <jimages123@gmail.com> - 2015-06-13 13:32 +0800
    Re: zip as iterator and bad/good practices Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-06-13 07:17 +0000
      Re: zip as iterator and bad/good practices Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-06-13 13:48 +0100
        Re: zip as iterator and bad/good practices Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-06-13 16:16 +0000

csiph-web