negative stride list slices

Alex Martelli aleaxit at yahoo.com
Fri Sep 3 02:11:35 EDT 2004


Shalabh Chaturvedi <shalabh at cafepy.com> wrote:
   ...
> Does anyone else find it intuitive to expect that S[b:a:-1] be exactly
> reverse of S[a:b:1]?

Not me, because I _know_ that S[x:y:anything] INCLUDES S[x] and EXCLUDES
S[y].  So, swappiing x and y can't POSSIBILY "exactly reverse' things,
*ever*, NO MATTER HOW 'anything' changes.

That's a good reason why 2.4 introduces the new built-in 'reversed': if
you want to loop on "exactly the reverse of" S[a:b], you loop on
reversed(S[a:b]) and live happily ever after.


Alex



More information about the Python-list mailing list