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


Groups > comp.lang.python > #92569

Re: zip as iterator and bad/good practices

X-Received by 10.13.212.209 with SMTP id w200mr21912214ywd.42.1434153744877; Fri, 12 Jun 2015 17:02:24 -0700 (PDT)
X-Received by 10.182.105.65 with SMTP id gk1mr139934obb.27.1434153744835; Fri, 12 Jun 2015 17:02:24 -0700 (PDT)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!peer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!q107no911223qgd.0!news-out.google.com!n7ni8481igk.0!nntp.google.com!h15no2330045igd.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Fri, 12 Jun 2015 17:02:24 -0700 (PDT)
In-Reply-To <mailman.447.1434152635.13271.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=15.219.169.68; posting-account=LpZxbAkAAADmdg1egflF6w8zbbyJPDNY
NNTP-Posting-Host 15.219.169.68
References <mles66$sk2$1@speranza.aioe.org> <201506122034.t5CKYYt0025588@fido.openend.se> <mailman.447.1434152635.13271.python-list@python.org>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <e5d750bd-d7e3-4d93-9794-e9f16a4b40bd@googlegroups.com> (permalink)
Subject Re: zip as iterator and bad/good practices
From sohcahtoa82@gmail.com
Injection-Date Sat, 13 Jun 2015 00:02:24 +0000
Content-Type text/plain; charset=ISO-8859-1
X-Received-Bytes 2034
X-Received-Body-CRC 3316751111
Xref csiph.com comp.lang.python:92569

Show key headers only | View raw


On Friday, June 12, 2015 at 4:44:08 PM UTC-7, Terry Reedy wrote:
> 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

On the second line of your final solution, is there any reason you're using `ints[:]` rather than just `ints`?

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