Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '(so': 0.07; 'explanation': 0.09; 'here?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'jan': 0.12; '#python': 0.16; '2.7.3': 0.16; 'comp': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'silly': 0.16; 'underlying': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'starts': 0.20; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'second': 0.26; 'header:X-Complaints-To:1': 0.27; 'header :In-Reply-To:1': 0.27; 'forgot': 0.30; 'that.': 0.31; '>>>>': 0.31; 'object.': 0.31; 'sep': 0.31; 'copying': 0.34; 'could': 0.34; 'object,': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'increased': 0.61; 'new': 0.61; 'received:173': 0.61; 'simply': 0.61; 'name': 0.63; 'more': 0.64; 'below.': 0.71; 'idiom': 0.84; 'received:fios.verizon.net': 0.84; '2013,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: L[:] Date: Fri, 10 Jan 2014 17:38:42 -0500 References: <1389375507.21198.YahooMailBasic@web163801.mail.gq1.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Gmane-NNTP-Posting-Host: pool-173-75-254-207.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 In-Reply-To: <1389375507.21198.YahooMailBasic@web163801.mail.gq1.yahoo.com> 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389393545 news.xs4all.nl 2843 [2001:888:2000:d::a6]:53530 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:63670 On 1/10/2014 12:38 PM, Albert-Jan Roskam wrote: > In Python Cookbook, one of the authors (I forgot who) consistently used= the "L[:]" idiom like below. If the second line simply starts with "L =3D= " (so no "[:]") only the name "L" would be rebound, not the underlying ob= ject. That was the author=C5=9B explanation as far as I can remember. I d= o not get that. Why is the "L[:]" idiom more memory-efficient here? How c= ould the increased efficiency be demonstrated? > > #Python 2.7.3 (default, Sep 26 2013, 16:38:10) [GCC 4.7.2] on linux2 >>>> L =3D [x ** 2 for x in range(10)] >>>> L[:] =3D ["foo_" + str(x) for x in L] Unless L is aliased, this is silly code. The list comp makes a new list=20 object, so if L does not have aliases, it would be best to rebind 'L' to = the existing list object instead of copying it. To do the replacement=20 'in place': L =3D [x ** 2 for x in range(10)] for i, n in enumerate(L): L[i] =3D "foo_" + str(n) print(L) >>> ['foo_0', 'foo_1', 'foo_4', 'foo_9', 'foo_16', 'foo_25', 'foo_36',=20 'foo_49', 'foo_64', 'foo_81'] --=20 Terry Jan Reedy