iterating in reverse

Mike Meyer mwm at mired.org
Fri Jan 17 12:47:24 EST 2003


jbperez808 at yahoo.com (Jonathan P.) writes:

> Erik Max Francis <max at alcyone.com> wrote in message news:<3E274D34.8F0C4348 at alcyone.com>...
> > "Jonathan P." wrote:
> > > Is there an elegant and
> > > efficient way to iterate through x in
> > > reverse without having to create a reversed
> > > copy of it (i.e. y=x[:]; y.reverse())?
> > 
> > It's not too elegant, but
> > 
> > 	for j in range(len(x) - 1, -1, -1):
> > 	    i = x[j]
> > 	    print i
> 
> This looks like the best bet so far short of using 2.3.
> One thing though, would using xrange instead of range
> be preferable?

Yes, but how efficient is range? Since it creates a list, it has to be
O(N) as well? xrange may solve that one, and certainly cuts down the
space it uses.

        <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.




More information about the Python-list mailing list