iterating in reverse

Carl Banks imbosol at vt.edu
Thu Jan 16 20:30:50 EST 2003


Chad Netzer wrote:
> FWIW. list.reverse() is often not too bad since it only has to do the 
> work of reversing the list indices in memory, not copying the actual 
> objects themselves.  Iterating backwards does save an O(N) operation, 
> but for small lists, list.reverse() is likely faster.

Iterating a list is an O(N) operation itself.  If .reverse() is faster
for a small list, it should also be faster for a large list.  The only
problem with .reverse() is you might want to keep the unreversed list
around.


-- 
CARL BANKS




More information about the Python-list mailing list