[Tutor] Speed of accessing list components

israel at uandmedance.com israel at uandmedance.com
Sun Jun 27 00:46:04 EDT 2004


Anyone know what the speed of something like the following would be?

 >>> alist = [1,2,3,4,5,6,7,8,9]
 >>>[alist[-(alist.index(a)+1)] for a in alist]
[9, 8, 7, 6, 5, 4, 3, 2, 1]




On Saturday, June 26, 2004, at 08:57 PM, Glen Wheeler wrote:

>> Are you specifically looking to print it or are you just looking to
>> reverse the list? Usually when Python handles things internally it is
>> quicker than doing it yourself. So I would have to say reversing the
>> list could best be done w/ the reverse() method.
>>
>> Alternatively as of Python 2.2, I believe, you can step through arrays
>> like so:
>>
>>>>> l
>> [1, 2, 3, 4, 5, 6]
>>>>> l[::-1]
>> [6, 5, 4, 3, 2, 1]
>>
>> The above is kind of like a slice. The syntax is:
>>
>> myList[start:end:step]
>>
>> Along the same idea there is also mapping and list comprehension.
>> According to O'Reily's Learning Python: 2nd Edition. map() is quicker
>> than 'for' and list comprehension is slightly quicker than map().
>>
>
>   Yes, list comprehensions permute to C code directly.  I did not know 
> about
> your slice-like example above -- thanks for the info.  I'll do some
> profiling later to see how it goes.
>   Reversing the list and then iterating through it (then reversing it 
> again)
> is really way too slow (time sensitive program).
>
>   Thanks,
>   Glen
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>



~Israel~
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 1614 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20040626/3840a4d6/attachment.bin


More information about the Tutor mailing list