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

Ian Foote ian at feete.org
Tue Mar 22 07:27:57 EDT 2016



On 22/03/16 11:05, BartC wrote:
> On 22/03/2016 01:01, Steven D'Aprano wrote:
>
>> Pythonic code probably uses a lot of iterables:
>>
>> for value in something:
>>      ...
>
>> in preference to Pascal code written in Python:
>>
>> for index in range(len(something)):
>>      value = something[index]
>
> (Suppose you need both the value and its index in the loop? Then the 
> one-line for above won't work. For example, 'something' is [10,20,30] 
> and you want to print:
>
>  0: 10
>  1: 20
>  2: 30 )
>

The builtin enumerate function is the idiomatic way:

     for index, item in enumerate(something):
         ...

Regards,
Ian F



More information about the Python-list mailing list