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


Groups > comp.lang.python > #92567

Re: zip as iterator and bad/good practices

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!bcyclone03.am1.xlned.com!bcyclone03.am1.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '-1,': 0.09; '[0,': 0.09; 'creighton': 0.09; 'iterate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'jan': 0.11; '-1):': 0.16; 'comp': 0.16; 'iterating': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.16; 'laura': 0.18; '>>>': 0.20; 'header:In-Reply-To:1': 0.24; 'header :User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'subject:/': 0.29; 'necessary,': 0.29; 'terry': 0.29; 'problem': 0.33; 'lists': 0.34; 'to:addr:python-list': 0.35; 'lists.': 0.35; 'list': 0.35; 'copying': 0.35; 'but': 0.36; 'subject:: ': 0.37; 'received:org': 0.38; 'end': 0.39; 'pm,': 0.39; 'things': 0.39; 'to:addr:python.org': 0.39; 'back': 0.61; 'real': 0.61; 'subject:good': 0.84; 'received:fios.verizon.net': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: zip as iterator and bad/good practices
Date Fri, 12 Jun 2015 19:43:08 -0400
References <mles66$sk2$1@speranza.aioe.org> <201506122034.t5CKYYt0025588@fido.openend.se>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-98-114-97-173.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0
In-Reply-To <201506122034.t5CKYYt0025588@fido.openend.se>
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.447.1434152635.13271.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1434152635 news.xs4all.nl 2955 [2001:888:2000:d::a6]:50917
X-Complaints-To abuse@xs4all.nl
X-Received-Bytes 3301
X-Received-Body-CRC 2621607177
Xref csiph.com comp.lang.python:92567

Show key headers only | View raw


On 6/12/2015 4:34 PM, Laura Creighton wrote:
> The real problem is removing things from lists when you are iterating
> over them, not adding things to the end of lists.

One needs to iterate backwards.

 >>> ints = [0, 1, 2, 2, 1, 4, 6, 5, 5]

 >>> for i in range(len(ints)-1, -1, -1):
	if ints[i] % 2:
		del ints[i]
	
 >>> ints
[0, 2, 2, 4, 6]

But using a list comp and, if necessary, copying the result back into 
the original list is much easier.

 >>> ints = [0, 1, 2, 2, 1, 4, 6, 5, 5]
 >>> ints[:] = [i for i in ints if not i % 2]
 >>> ints
[0, 2, 2, 4, 6]


-- 
Terry Jan Reedy

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