negative stride list slices
shalabh at cafepy.com
Thu Sep 2 18:54:11 CEST 2004
Christopher T King wrote:
> On Wed, 1 Sep 2004, Shalabh Chaturvedi wrote:
>>Does anyone else find it intuitive to expect that S[b:a:-1] be exactly
>>reverse of S[a:b:1]?
> No. S[a:b:1] includes the start index (a), but excludes the stop index
> (b). Similarly, S[b:a:-1] includes the start index (b), but excludes the
> stop index (a). Only if the stop indices were included (or the start
> indices were excluded), thus making the indexing symmetrical, would I
> expect the results to be symmetric.
That's a good explanation. My initial confusion arose because I
remembered how slices work by the suggestion (and diagram) under:
"The best way to remember how slices work is to think of the indices as
pointing /between/ characters". When negative step is in the picture,
the indices are no longer in the same inbetween places and need to be
shifted to the right. Associating indices with characters themselves
dissipates the confusion.
More information about the Python-list