[Tutor] Speed of accessing list components

Glen Wheeler gew75 at hotmail.com
Sat Jun 26 23:57:33 EDT 2004


> 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



More information about the Tutor mailing list