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


Groups > comp.lang.python > #92572

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!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.013
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'matches': 0.07; '[0,': 0.09; 'cc:addr:python-list': 0.10; 'jan': 0.11; 'def': 0.14; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'reedy': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'fairly': 0.22; 'am,': 0.23; '2015': 0.23; 'references': 0.23; 'sat,': 0.23; 'header:In-Reply-To:1': 0.24; 'second': 0.24; 'message-id:@mail.gmail.com': 0.28; 'subject:/': 0.29; '13,': 0.29; 'behaviour': 0.29; 'solution,': 0.29; 'terry': 0.29; 'similar': 0.32; 'received:google.com': 0.34; 'list': 0.35; 'but': 0.36; 'list,': 0.36; 'there': 0.36; 'two': 0.37; 'subject:: ': 0.37; 'list.': 0.37; 'rather': 0.38; 'your': 0.60; 'entire': 0.61; 'chrisa': 0.84; 'subject:good': 0.84; 'to:none': 0.90
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=5MM7XVUYP7qadmj5ocnM9FpKqv2RfJqV0bs2TQkt9Lg=; b=Ig4jQuuBgUJwKfr2/pk1Q8bQ+6LV8R2r70unwMuvw4xgv6+tdFQih7VtL4mqC5NvD0 Odj3kF7WAO2jFAVfXscg5D6sHQnWqvFVG9Mdksg0SO0EBvZuRQvpcVfms/S5VmKFFRSj 8N5FHAij3Sah7q6k226aVh1XSp212x/vBunh7MRSwPremuTtbjssZaRn+72823HfQTOd YdCL0MVCIa/FH8vlVOMaGQEKMCAij+eBW1sJlbQF1Qb3VRqlBK/flbla68wKco0QGEDF bUJWD/OCirhZgKE/Is7L2fJpm3jbLiGmBJH+X9jVTIIflJ+IWPGwut+PRKT4wtWI24vV OGuA==
MIME-Version 1.0
X-Received by 10.50.176.228 with SMTP id cl4mr7819285igc.2.1434155209433; Fri, 12 Jun 2015 17:26:49 -0700 (PDT)
In-Reply-To <e5d750bd-d7e3-4d93-9794-e9f16a4b40bd@googlegroups.com>
References <mles66$sk2$1@speranza.aioe.org> <201506122034.t5CKYYt0025588@fido.openend.se> <mailman.447.1434152635.13271.python-list@python.org> <e5d750bd-d7e3-4d93-9794-e9f16a4b40bd@googlegroups.com>
Date Sat, 13 Jun 2015 10:26:49 +1000
Subject Re: zip as iterator and bad/good practices
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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.450.1434155212.13271.python-list@python.org> (permalink)
Lines 22
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1434155212 news.xs4all.nl 2870 [2001:888:2000:d::a6]:37577
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:92572

Show key headers only | View raw


On Sat, Jun 13, 2015 at 10:02 AM,  <sohcahtoa82@gmail.com> wrote:
>>  >>> 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`?

If you use "ints = [...]", it rebinds the name ints to the new list.
If you use "ints[:] = [...]", it replaces the entire contents of the
list with the new list. The two are fairly similar if there are no
other references to that list, but the replacement matches the
mutation behaviour of remove().

def just_some(ints):
    ints[:] = [i for i in ints if not i % 2]

ChrisA

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