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


Groups > comp.lang.python > #63888

Re: L[:]

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <fomcl@yahoo.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; ';-)': 0.03; 'else:': 0.03; 'assignment': 0.07; 'modify': 0.07; 'paths': 0.07; 'below)': 0.09; 'inserts': 0.09; 'item,': 0.09; 'iterate': 0.09; 'creates': 0.14; '(however,': 0.16; 'appreciated!': 0.16; 'copied.': 0.16; 'deletes': 0.16; 'shallow': 0.16; 'silly': 0.16; 'essential': 0.16; 'modification': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'slightly': 0.19; 'case.': 0.24; 'first,': 0.26; 'nearly': 0.26; 'second': 0.26; 'somewhere': 0.26; 'header:In-Reply-To:1': 0.27; 'to:2**1': 0.27; 'point': 0.28; '[1]': 0.29; 'am,': 0.29; '[2]': 0.30; 'code': 0.31; 'object.': 0.31; 'probably': 0.32; 'another': 0.32; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'replies': 0.36; 'right?': 0.36; 'hi,': 0.36; 'so,': 0.37; 'two': 0.37; 'list': 0.37; 'clear': 0.37; 'thank': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'even': 0.60; 'received:98.137': 0.60; 'most': 0.60; 'new': 0.61; 'matter': 0.61; 'name:': 0.61; 'making': 0.63; 'more': 0.64; 'within': 0.65; 'effectively': 0.66; 'received:gq1.yahoo.com': 0.68; 'received:mail.gq1.yahoo.com': 0.68; 'received:98.136': 0.69; '4:00': 0.84; 'nagy': 0.84; 'received:bullet.mail.gq1.yahoo.com': 0.84
X-Yahoo-Newman-Property ymail-3
X-Yahoo-Newman-Id 89544.37962.bm@omp1082.mail.gq1.yahoo.com
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1389696127; bh=ruhMC5sLTopSHLOKaC44ae6QZ1XdYTGUX7fD2FUdf7k=; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:Message-ID:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=CPYQk+Eoan8cuW+57EQxeXeFwYFyIHX+gyASw4Rjg4g1+cTefi/Aal1ZjcjTcoAJpoJ7utCVuAmbaJ/Po1jEom9Kn+LmUHt8iYcMZdaoFYFU/dmvc40QvivYNm5ejIY7NhlQsLCcAuCj43AMgFPOc6fXFkHoGKOMEO/uuQc2fW4=
DomainKey-Signature a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:Message-ID:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=C3VfMfevg7xglvB/I6R+3sc6xuvpAjHYqoyL5bzTKHx4eYCVFBhE1iTGtdOoHs/rwU4cFkkplRw0IX3MxwHP1rVqyQRAFQEwawxRqXwe9O7Qm5/eIbcRDzzPRO8EuvnWDw8uERhY1DRW5tRiDLjLTrM6KPvHI3QwNXtlaH+3U6k=;
X-YMail-OSG s7ezWU0VM1lJ8mCTRMNyJ5SVf8ZFHEt1kbGA_SfKVe9kDh2 HSKTmQxM6Tkpm8LcHgXZZBf6h4ohsfu5jYv33upXj3Z6YbxhG4M9d8sTNWAi VTPNfZXQYL0urtiKBTztog8OyROguN2X_R0v8v_T_Awye9WZBXhui0N9TDWh d.rvN_6a90k_fMbY5dfvCaP9oHJvt_Qfc77etD6SYFD3g8VSV6UNil.Z9PXl p57spcVasyZye37LuIBarp55upL755s_W8OwfsrqyOw5B0HBXQziPC7BqiLR aQgHUVejUfJFHrXjXZn4Bt3M7Z_IfOn8rz2mW3xPGm_iveJ6FlAlHqOLrz9i JEF_myzpSS9l7t1MPwAK3wvZ82G2KvR0wG.yiojirmz5EA.vw9WmeO3v1Q_Y Vx0jGU9tFF41DvRsmeWpLMg8mhGAvKRya7.9uWkjiWCDCVvIhlDQUnXd_.ao Lz_v5lOpo4GjhBcwTRwEDO7GLE0oU8D0SHwZeRrVoOCwUGfsp0yiBGTkxz2M Zxwgms_SdF23P5uOVZIL2nFuOXAbU2totoVwv09DlcbraHyPxvSP8dvb_YUI imsh9dpYR
X-Rocket-MIMEInfo 002.001, IE9uIDEvMTMvMjAxNCA0OjAwIEFNLCBMYXN6bG8gTmFneQ0KIHdyb3RlOg0KID4gDQogPj4gVW5sZXNzIEwgaXMgYWxpYXNlZCwgdGhpcyBpcyBzaWxseSBjb2RlLg0KID4gVGhlcmUgaXMgYW5vdGhlciB1c2UgY2FzZS4gSWYgeW91IGludGVuZCB0byBtb2RpZnkgYQ0KIGxpc3Qgd2l0aGluIGEgZm9yDQogPiBsb29wIHRoYXQgZ29lcyBvdmVyIHRoZSBzYW1lIGxpc3QsIHRoZW4geW91IG5lZWQgdG8NCiBpdGVyYXRlIG92ZXIgYSBjb3B5Lg0KID4gQW5kIHRoaXMgY2Fubm90IGJlIGNhbGxlZCBhbiAiYWxpYXMBMAEBAQE-
X-Mailer YahooMailClassic/387 YahooMailWebService/0.8.172.614
Date Tue, 14 Jan 2014 02:42:07 -0800 (PST)
From Albert-Jan Roskam <fomcl@yahoo.com>
Subject Re: L[:]
To python-list@python.org, Terry Reedy <tjreedy@udel.edu>
In-Reply-To <lb1hvc$cog$1@ger.gmane.org>
MIME-Version 1.0
Content-Type text/plain; charset=iso-8859-1
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.5450.1389696235.18130.python-list@python.org> (permalink)
Lines 48
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1389696236 news.xs4all.nl 2927 [2001:888:2000:d::a6]:59087
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:63888

Show key headers only | View raw


 On 1/13/2014 4:00 AM, Laszlo Nagy
 wrote:
 > 
 >> Unless L is aliased, this is silly code.
 > There is another use case. If you intend to modify a
 list within a for
 > loop that goes over the same list, then you need to
 iterate over a copy.
 > And this cannot be called an "alias" because it has no
 name:
 
 for i in somelist: creates a second reference to somelist
 that somewhere in the loop code has a name, so it is
 effectively an 'alias'. The essential point is that there
 are two access paths to the same object.
 
 > for idx,item in enumerate(L[:]):
 >     # do something with L here,
 including modification
 
 The copy is only needed in the above if one inserts or
 deletes. But if one inserts or deletes more than one item,
 one nearly always does better to iterate through the
 original and construct a new list with new items added and
 old items not copied.
 

====> Hi, first, thank you all for your replies -much appreciated!
Terry, this would be making a shallow copy, right? If so, then "list(L)" is slightly nicer IMHO, but that's probably a matter of taste (however, I don't like copy.copy, even though that's perhaps most clear --oh well nitpicking ;-)

I also found that item assignment ([1] below) is much faster than using the more standard (I think) .append ([2]).
# [1]
for idx,item in enumerate(L[:]):
   if some_condition: 
      L[idx] = foobarify(item)
# [2]
L2 = []     
for idx,item in enumerate(L):
   if some_condition: 
       L2.append(foobarify(item))
   else:
       L2.append(item)

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


Thread

Re: L[:] Albert-Jan Roskam <fomcl@yahoo.com> - 2014-01-14 02:42 -0800

csiph-web