negative stride list slices

Michael Hudson mwh at
Thu Sep 2 15:21:09 CEST 2004

dave at (David Abrahams) writes:

> Can anyone explain the logic behind the behavior of list slicing with
> negative strides?  

You can read PySlice_GetIndicesEx in Objects/sliceobject.c if you're
really keen...

> For example:
>   >>> print range(10)[:-3:-1]
>   [9,8]
> I found this result very surprising,

What would you have expected instead?

range(10-1, 10-3, -1) == [9,8]

was my guiding principle when I wrote the code.

> and would just like to see the rules written down somewhere.

It seems to be hardly any easier to write the rules than to write the
algorithm, in practice...

In practice, I only ever use negative strides as "[::-1]", the result
of which hopefully surprises noone.


  <glyph> yo ho ho and a bottle of internet
                                                -- from Twisted.Quotes

More information about the Python-list mailing list