[Tutor] Speed of accessing list components

Dragonfirebane at aol.com Dragonfirebane at aol.com
Sat Jun 26 10:08:34 EDT 2004


In a message dated 6/25/2004 8:18:08 PM Eastern Standard Time, 
gew75 at hotmail.com writes:

  Hi all,

  This is mainly for curiosity's sake, but when accessing a list component
using the -i method of indexing, in e.g.

>>> l
[1, 2, 3, 4, 5, 6]
>>> for i in range(1, len(l)+1):
..  print l[-i]
..
6
5
4
3
2
1

  is this exceptionally slow?  Or the preferred method?  In particular, I do
not want to reverse the list (this would add greater complexity to other
parts of the program) yet I don't really like the expression ``range(1,
len(l)+1)''.

  Thanks guys,
  Glen

_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor
It seems to me that one easy(er) way of doing this would be:

for i in l:
    print l[-i]

which would achieve the same effect with no discernable slowdown and a less 
awkward range.

Email: dragonfirebane at aol.com
AIM: singingxduck
Programming Python for the fun of it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20040626/f7a1acb8/attachment.html


More information about the Tutor mailing list