iterating in reverse

Chad Netzer cnetzer at mail.arc.nasa.gov
Wed Jan 22 03:45:38 CET 2003


On Monday 20 January 2003 17:07, Jonathan P. wrote:
> Chad Netzer <cnetzer at mail.arc.nasa.gov> wrote in message
> news:<mailman.1042798054.23362.python-list at python.org>...
>
> > ~~~~~~~~~~~~~
> > seq = "the quick brown fox jumped over the lazy dog"
> > for i in xrange( len( seq ) ):
> >    forward_iter = seq[i]
> >    backward_iter = seq[-(i + 1)]
> >    pass # do whatever you need
> > ~~~~~~~~~~~~~
> >
> > In this case, you don't copy or generate any new lists at all, so
> > it should be quite quick.
>
> Chad, I think this is the best solution so far.
> Clean and fast and _obvious_.  So much so that I'd
> like to kick myself for not coming up with that
> sooner. Sigh...

Thanks, and glad to be of help.  Most of the credit should go to
whomever I stole the idea from. :)  (Paul Rubin, I think)

-- 
Bay Area Python Interest Group - http://www.baypiggies.net/

Chad Netzer






More information about the Python-list mailing list