Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: BartC Newsgroups: comp.lang.python Subject: Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?) Date: Thu, 24 Mar 2016 14:16:06 +0000 Organization: A noiseless patient Spider Lines: 46 Message-ID: References: <8737rvxs89.fsf@elektro.pacujo.net> <56e7483d$0$1608$c3e8da3$5496439d@news.astraweb.com> <56ef9787$0$1516$c3e8da3$5496439d@news.astraweb.com> <56f02196$0$1588$c3e8da3$5496439d@news.astraweb.com> <56f3f09a$0$1595$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 24 Mar 2016 14:13:02 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="cf45b3961a050227b1103bebc3cbc15a"; logging-data="13508"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+1eN1qR4BZgZGDSL7DMvkA" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.0 In-Reply-To: Cancel-Lock: sha1:nK59hBA4S2qeThLxt86YmaVSttY= Xref: csiph.com comp.lang.python:105614 On 24/03/2016 14:08, Jon Ribbens wrote: > On 2016-03-24, BartC wrote: >> On 24/03/2016 13:50, Steven D'Aprano wrote: >>> Likewise clearing a list: >>> >>> for i in range(len(mylist)-1, -1, 0): >>> del mylist[i] >> >> That's wouldn't be I'd call clearing a list, more like destroying it >> completely! >> >> How would you actually clear a list by traversing it (ie. not just >> building a new one)? >> >> This doesn't work: >> >> for x in L: >> x=0 >> >> as each x only refers to the value in each element of L, not the element >> itself (like the pass-by-reference problem). >> >> I'd presumably have to do: >> >> for i in range(len(L)): >> L[i]=0 > > That's kind've a weird thing to want to do; The thing I'm trying to demonstrate is changing an element of a list that you are traversing in a loop. Not necessarily set all elements to the same value. if you thought you needed > to do that then most likely what you should actually be doing is > re-writing your code so you no longer need to. However, you could do: > > L[:] = [0] * len(L) OK, but that's just building a new list as I've already mentioned. Or is the Pythonic way, when you want to change some elements of a list to just build a new one that incorporates those changes? -- Bartc