7

I noticed, given l = [1,2,3], that l[-1:] returns [3] as expected, but that l[-1:0] returns [], very much unlike what I expected. I then tried [-1:1], which I expected to return [3,1], but it also returns [].

Is there a good reason why the slice syntax does not wrap around from negative to positive indices (and the other way round)?

It seems it would be pretty useful and pretty straightforward to implement, but maybe I'm missing something.

3
  • Note that you can pass a step option, such as l[-1:1:-1] to step backward one element at a time. Commented Sep 30, 2014 at 10:15
  • 2
    I guess it would be ambiguous. If it can "wrap around", then [-1:1] could result in [3,1,2,3,1] as well. Also, that way is would be pretty much impossible to get an empty slice! E.g., should [2:1] also "wrap around", or produce an empty slice? Things like [-3:2] work, as long as the first index (counting from whatever end) is smaller than the second index. Commented Sep 30, 2014 at 10:25
  • 1
    @tobias_k I don't see how it would be ambiguous? Slices always go from left to right, as I understand them. How could l[-1:1] then result in [3,1,2,3,1] as you say? Commented Sep 30, 2014 at 11:11

3 Answers 3

3

Slicing has a very simple definition: you go from start by jumps of step as long as you are below stop. If start or step are negative, first add the length of the array.

One time your suggestion causes irksome behaviour is:

x[10:-10] 

If x[-10] is after x[10], you want the slice from x[10] to x[len(x)-10-1]. If you have wrap-around, you'll have the slice x[10:] + x[:-10], which is mostly useless.

Wrap-around behaviour is easy to emulate (eg x[m:] + x[:n]) with the current behaviour, but the current, more useful, behavior is hard to emulate with wrap-around.

Sign up to request clarification or add additional context in comments.

3 Comments

Well, I would call it a feature instead of "irksome behaviour". It would simply be a generalization of the current behaviour.
@rkrzr The point is that it's simply not useful the vast majority of the time. Having cyclical behaviour would imply that large indices would also wrap around, but that can cause loads of silent bugs. I can't see the benefits.
Fair enough - to me it seems more elegant, but I agree that it would be seldomly useful.
2

Guido van Rossum, the original inventor of Python language, has an extensive blog post about the slice syntax here:

http://python-history.blogspot.fi/2013/10/why-python-uses-0-based-indexing.html

I do not believe there is any particular reason why it doesn't work like that. Maybe the thought didn't cross Guido's mind when he was writing the initial syntax, maybe because of clarity. In any case it is too late to add such new features to Python syntax as it could break a lot of existing software.

Comments

0

First you need to know that Slicing in python is from left to right So when you try [3,1] or [-1:1] result is empty ! You can suppose that every entry in list have 2 index that shows in below :for exam ple if we have a list with 4 entry this is the indexes numbers :

[0,1,2,3,4] 

or

[-5,-4,-3,-1] 

for complete understanding of slicing read have a look at here :http://structure.usc.edu/numarray/node26.html

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.