[Tutor] Speed of accessing list components

Alan Gauld alan.gauld at blueyonder.co.uk
Sun Jun 27 05:08:25 EDT 2004


> >>> l
> [1, 2, 3, 4, 5, 6]
> >>> for i in range(1, len(l)+1):
> ..  print l[-i]
> ..
> parts of the program) yet I don't really like the expression
``range(1,
> len(l)+1)''.

You can probably speed it up slightly by using the range step value:

for i in range(len(l)-1,0,-1):
   print l[i]

Simply because you don't need to perform he index negation each time.

Alan G.




More information about the Tutor mailing list