Slicing with negative strides

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Oct 29 07:04:21 EDT 2013


On Tue, 29 Oct 2013 08:53:08 +0000, Duncan Booth wrote:

> Steven D'Aprano <steve at pearwood.info> wrote:
> 
>> Does anyone here use slices (or range/xrange) with negative strides
>> other than -1?
>> 
>> E.g. sequence[2:15:-3]
> 
> With any negative stride your example is just the empty sequence.

Gah, sorry about that, that's the suggested *new* syntax. Possibly my 
subconscious likes it better than my conscious :-)

Try this instead: sequence[15:2:-3]


>> If so, there is a discussion (long, long, looooooong discussion) on the
>> python-ideas mailing list, debating whether or not to deprecate or
>> change the behaviour of slicing with negative strides. So if you care
>> about the current behaviour, now is the time to stand up and be
>> counted.
>> 
>> (Standing up *here* is fine, don't feel that you have to join yet
>> another list.)
>> 
> For those of us that don't really want to join another mailing list,
> could you summarise what change is being proposed?

* Negative strides should be deprecated and then removed.

* Or just deprecated.

* Or change the semantics of negative strides so that 
  seq[2:15:-2] works as expected.

* Or get rid of negative indexing.

* Or add new syntax to control whether or not the end points are included.

* Or ... 


It's Python-Ideas, otherwise known as Bike-Shed Central :-)

I think the main idea which is likely (since Guido seems to be slightly 
leaning that way) is to deprecate negative strides, remove them in a 
release or three, and then re-introduce them in Python 4000 but with more 
intuitive semantics.



-- 
Steven



More information about the Python-list mailing list