Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Slicing with negative strides

78 views
Skip to first unread message

Steven D'Aprano

unread,
Oct 29, 2013, 1:22:00 AM10/29/13
to
Does anyone here use slices (or range/xrange) with negative strides other
than -1?

E.g. sequence[2:15:-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.)



--
Steven

Duncan Booth

unread,
Oct 29, 2013, 4:53:08 AM10/29/13
to
Steven D'Aprano <st...@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.

>
>
> 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?


--
Duncan Booth http://kupuguy.blogspot.com

Mark Lawrence

unread,
Oct 29, 2013, 4:54:34 AM10/29/13
to pytho...@python.org
On 29/10/2013 05:22, Steven D'Aprano wrote:
> Does anyone here use slices (or range/xrange) with negative strides other
> than -1?
>
> E.g. sequence[2:15:-3]
>

In 10 ish years I don't recall ever considering it, let alone doing it.

--
Python is the second best programming language in the world.
But the best has yet to be invented. Christian Tismer

Mark Lawrence

Mark Lawrence

unread,
Oct 29, 2013, 5:34:56 AM10/29/13
to pytho...@python.org
Umpteen options have been put forward. IMHO the bookies favourite is
currently being endorsed by Tim Peters and Terry Reedy, yours odds may
vary :)

Terry Reedy

unread,
Oct 29, 2013, 6:21:15 AM10/29/13
to pytho...@python.org
On 10/29/2013 4:53 AM, Duncan Booth wrote:
> Steven D'Aprano <st...@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.

The idea is that one would not have to reverse 2 and 15 to get a
non-empty sequence.

--
Terry Jan Reedy

Steven D'Aprano

unread,
Oct 29, 2013, 7:04:21 AM10/29/13
to
On Tue, 29 Oct 2013 08:53:08 +0000, Duncan Booth wrote:

> Steven D'Aprano <st...@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

Martin Manns

unread,
Nov 3, 2013, 6:15:40 PM11/3/13
to
On 29 Oct 2013 05:22:00 GMT
Steven D'Aprano <st...@pearwood.info> wrote:

> Does anyone here use slices (or range/xrange) with negative strides
> other than -1?

I have used negative strides for comparing discrete sequences e. g. for
turbulence analysis, and I hope that my code will still run in Python 4.

Martin

Steven D'Aprano

unread,
Nov 4, 2013, 10:18:31 AM11/4/13
to
Can you show us a typical example of how you would normally use such
negative strides?


Thanks,



--
Steven

Peter Cacioppi

unread,
Nov 6, 2013, 2:15:02 PM11/6/13
to
On Monday, October 28, 2013 10:22:00 PM UTC-7, Steven D'Aprano wrote:
> Does anyone here use slices (or range/xrange) with negative strides other
>
> than -1?
>

Non default positive strides are very handy, but negative strides seem weird to me. Not the negative striding exactly, but the way fenceposts and negative strides interact.

For example, this poster seems to posit a canonical WTF with negative strides.

http://stackoverflow.com/questions/5798136/python-reverse-stride-slicing

I can almost picture Picard saying "WTF do you need to omit the end index to get the zero element??!!"

Readability counts, no? Just reverse it and use positive strides.





0 new messages