iterating in reverse
Michael Hudson
mwh at python.net
Fri Jan 17 06:17:08 EST 2003
Paul Rubin <phr-n2002b at NOSPAMnightsong.com> writes:
> Andrew Bennetts <andrew-pythonlist at puzzling.org> writes:
> > > will always print x's contents in the
> > > same sequence. 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())?
> >
> > Get the Python 2.3 alpha, and it's really easy :)
> >
> > >>> l = [1,2,3,4,5]
> > >>> for i in l[::-1]:
> > ... print i
>
> My guess is that the above creates a reversed copy.
Yes, like all slices (of the builtin list type, anyway) this creates a
copy.
Cheers,
M.
--
Ya, ya, ya, except ... if I were built out of KSR chips, I'd
be running at 25 or 50 MHz, and would be wrong about ALMOST
EVERYTHING almost ALL THE TIME just due to being a computer!
-- Tim Peters, 30 Apr 97
More information about the Python-list
mailing list