The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)
Jussi Piitulainen
jussi.piitulainen at helsinki.fi
Thu Mar 24 10:34:13 EDT 2016
BartC writes:
> On 24/03/2016 14:08, Jon Ribbens wrote:
>> On 2016-03-24, BartC wrote:
>>> 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.
You understand correctly, but it may be more natural in practice to
write it this way:
for k, item in enumerate(them):
them[k] = f(item)
I _think_ I might write it that way even when "f(item)" does not depend
on the old value at all, but I don't expect to be in that situation.
More information about the Python-list
mailing list