It starts counting from "2" going backwards (step -1) until "-5" is reached - but reaching the element "-5" would require positive step in this case.

For example the output of:

i[2::-1]

is: [2, 1, 0]

Answer from Lemurata on Stack Overflow
🌐
Python Examples
pythonexamples.org › python-range-with-negative-step
Python Range with Negative Step Value
For this specific scenario we are discussing, where step is a negative value, start must be greater than or equal to stop. In this example, we shall iterate over a range that starts at 9 and goes until 4 in steps of -2. For example, in the following program, we define a range from 9 to 4 with ...
Discussions

Python range() with negative strides - Stack Overflow
(Also note that you need to have the "stop" parameter at the next number in the negative sequence, -11, from what you wanted, -10, just like in a standard, ascending range.). ... Save this answer. ... Show activity on this post. Yes, however you'll need to specify that you want to step backwards ... More on stackoverflow.com
🌐 stackoverflow.com
python - understanding negative slice step value - Stack Overflow
The reason why f[0:5:-1] does not ... so Python returns an empty string. Instead, you want f[5:0:-1], which returns the string "raboo". Notice that the string does not contain the f character. To do that, you'd want f[5::-1], which returns the string "raboof". ... I have read start should not pass stop. is that true in case of negative step value ... More on stackoverflow.com
🌐 stackoverflow.com
What's the logic behind stop value of range function with negative step value in python - Stack Overflow
1 Seeking clarification on the in range function in Python where the stop value is smaller than the start value · 0 Why can't we get the range of a particular negative number · 1 Range() including its bounds for positive and negative steps More on stackoverflow.com
🌐 stackoverflow.com
python - Understanding negative steps in list slicing - Stack Overflow
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 ex... More on stackoverflow.com
🌐 stackoverflow.com
🌐
LabEx
labex.io › tutorials › python-how-to-use-negative-step-in-range-418732
Python - How to use negative step in range()
## Basic negative step example reverse_sequence = range(10, 0, -1) for num in reverse_sequence: print(num)
🌐
TutorialKart
tutorialkart.com › python › python-range › python-range-reverse-negative-step
Python range() - Reverse or Negative Steps
January 17, 2021 - If we are taking steps in the backward direction, start argument should be greater than stop to generate any elements in the range object. And step should be a negative number. In the following example, we shall generate a Python Range with ...
🌐
YouTube
youtube.com › wibyte
Python Programming| How to slice strings with a negative step size? - YouTube
In this video, we explore string slicing with negative index values. Using negative value in the step size has the impact of reversing the (entire) or part o...
Published: May 20, 2022
Views: 539
Find elsewhere
🌐
Finxter
blog.finxter.com › home › learn python blog › introduction to slicing in python
Introduction to Slicing in Python - Be on the Right Side of Change
March 8, 2023 - If the step size is set to the default value 1, we can use the single colon notation for brevity. This is an interesting feature in Python. A negative step size indicates that we are not slicing from left to right but from right to left.
🌐
USAVPS
usavps.com › home › python › python tutorial: how to understand negative step in slicing in python?
Python Tutorial: How to Understand Negative Step in Slicing in Python? - USAVPS
November 8, 2024 - Slicing in Python is performed ... By default, start is 0, stop is the length of the sequence, and step is 1. A negative step allows you to slice a sequence in reverse order....
🌐
Knowledgehills
knowledgehills.com › python › negative-indexing-slicing-stepping-comparing-lists.htm
Python Lists – Negative Indexing, Slicing, Stepping, Comparing, Max and Min – Knowledge Hills
The output of this third print statement will print all elements starting from beginning element to the last element in a step of 2. Basically the all alternate elements of the list. The fifth print combines slicing with a negative step. The output of this third print statement will print all elements starting from ending element to the beginning element. Basically the reverse of the list. This stackoverflow discussion is very useful if you like to understand more about slicing. Also this python doc has the official definition.
Top answer
1 of 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.

2 of 2
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]
🌐
Python.org
discuss.python.org › python help
Negative Slicing in Python - Python Help - Discussions on Python.org
September 8, 2023 - In this code, -1 index is 3, and -2 index is 2, right? But since we don’t include the second index itself, then we only have one value which is 3. However, why is the result an empty list? nums = [1,2,3] vals = nums [-1…
🌐
YouTube
youtube.com › shorts › R86tPPGtIRM
How to use negative step in Range function in Python #coding #pythonprogramming #dataanalytics - YouTube
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
Published: November 25, 2023
🌐
Python Examples
pythonexamples.org › python-range
range() Built-in Function -
Usually, when step is negative, start is greater than the stop, and r[i] should be greater than stop.
🌐
GeeksforGeeks
geeksforgeeks.org › slicing-with-negative-numbers-in-python
Slicing with Negative Numbers in Python | GeeksforGeeks
December 2, 2024 - Start and end can be any valid index whether it is negative or positive. When we write step as -1 it will reverse the slice.
🌐
Learn By Example
learnbyexample.org › python-string-slicing
Python String Slicing - Learn By Example
April 20, 2020 - You can even specify a negative step size. # Returns every 2nd item between position 6 to 1 in reverse order S = 'ABCDEFGHI' print(S[6:1:-2]) # GEC
🌐
GitHub
github.com › modular › modular › discussions › 2234
Fix List slicing and String slicing to match Python when step is negative · modular/modular · Discussion #2234
April 7, 2024 - By preserving the programmers intent to include the end of the range it can correctly handle both unspecified start or end as well as negative start or end. Whichever direction you are slicing in the start index is inclusive while the end index is exclusive. So with step = -1, end = -1 if given by the programmer meanssize - 1 but if the programmer has specified end = None then Slice.end needs to be -1 so that the 0 index value is included in the slice.
Author: modular