The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

BartC bc at freeuk.com
Thu Mar 24 10:49:17 EDT 2016


On 24/03/2016 14:34, Jussi Piitulainen wrote:
> 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.
>

Yes, you're right. Usually the update is conditional on the existing 
value. But my too-simple example would have had an unneeded item variable.

-- 
Bartc



More information about the Python-list mailing list