10

I am trying to understand the following behavior and would welcome any references (especially to official docs) or comments.

Let's consider a list:

>>> x = [1,2,3,4,5,6] 

This works as expected

>>> x[-1:-4:-1] [6, 5, 4] 

But I am surprised the following is empty:

>>> x[0:-4:-1] [] 

Consequently, I am surprised the following is not empty

>>> x[0:-len(x)-1:-1] > [1] 

especially given that

>>> x[0:-len(x):-1] [] 

and also that

>>> x[0:-len(x)-1] [] 

is empty.

2 Answers 2

8

I was pointed to the reference implementation (hattip to the Anonymous Benefactor) and found that it is fairly straightforward to understand the behavior from there. To be complete, IMHO this behavior is unintuitive, but it nevertheless is well defined and matches the reference implementation.

Two CPython files are relevant, namely the ones describing list_subscript and PySlice_AdjustIndices. When retrieving a slice from a list as in this case, list_subscript is called. It calls PySlice_GetIndicesEx, which in turn calls PySlice_AdjustIndices. Now PySlice_AdjustIndices contains simple if/then statements, which adjust the indices. In the end it returns the length of the slice. To our case, the lines

if (*stop < 0) { *stop += length; if (*stop < 0) { *stop = (step < 0) ? -1 : 0; } } 

are of particular relevance. After the adjustment, x[0:-len(x)-1:-1] becomes x[0:-1:-1] and the length 1 is returned. However, when x[0:-1:-1] is passed to adjust, it becomes x[0:len(x)-1:-1] of length 0. In other words, f(x) != f(f(x)) in this case.

It is amusing to note that there is the following comment in PySlice_AdjustIndices:

/* this is harder to get right than you might think */ 

Finally, note that the handing of the situation in question is not described in the python docs.

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

1 Comment

reference to the original code is very helpful for clarity of the concept
6

The fact that

> x[-1:-4:-1] [6, 5, 4] > x[0:-4:-1] [] 

should not surprise you! It is fairly obvious that you can slice a list from the last to the fourth-last element in backwards steps, but not from the first element.

In

x[0:i:-1] 

the i must be < -len(x) in order to resolve to an index < 0 for the result to contain an element. The syntax of slice is simple that way:

x[start:end:step] 

means, the slice starts at start (here: 0) and ends before end (or the index referenced by any negative end). -len(x) resolves to 0, ergo a slice starting at 0 and ending at 0 is of length 0, contains no elements. -len(x)-1, however, will resolve to the actual -1, resulting in a slice of length 1 starting at 0.

Leaving end empty in a backward slice is more intuitively understood:

> l[2::-1] [3, 2, 1] > l[0::-1] [1] 

2 Comments

Thanks for the answer, but I still don't get it. You write that -len(x)-1 resolves to -1. Could you please elaborate on how it resolves to -1 and why, since for me -len(x)-1 seems to equal -7. Is it related to the fact that x[0] == x[-len(x)]?
Yeah. Any negative number -i used as an index or slice param on a sequence seq resolves to the actual index len(seq) - i. Thus for a list of length 6, l[0:-7:-1] is a slice from 0 to -1 (6 - 7) in backwards steps. For any backwards slice containing the first element (index 0), you have to pick end < -len(seq) or leave it empty: seq[start::-1]!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.