The problem is that Python doesn’t switch the direction of iteration - the step is still +1. Your slice means to start at index -1 (aka len()-1=2), and then add 1 until the index is >= -2. Which is true immediately, so the result is empty. If you use a negative step to go the other direction, you ge… Answer from TeamSpen210 on discuss.python.org
🌐
GeeksforGeeks
geeksforgeeks.org › python › slicing-with-negative-numbers-in-python
Slicing with Negative Numbers in Python - GeeksforGeeks
September 18, 2025 - In Python, negative slicing can be done in two ways: Using colon (:) operator · Using slice() function · Both methods allow us to specify the start, end and step positions using negative indices.
🌐
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…
Discussions

List slicing with negative index
See this stackoverflow thread: Understanding slice notation More on reddit.com
🌐 r/learnpython
5
2
December 15, 2021
Negative slicing Python
Why in negative slicing in my screenshot the last index value is not shown? Someone explain me. I am having some confusion on last index one. fav_fruit = "mango" print(fav_fruit[-3:-1]) here it prints as: ng why the letter “o” is not printed? More on discuss.codecademy.com
🌐 discuss.codecademy.com
1
0
May 10, 2020
String Slicing with Negatives in Python - Stack Overflow
lets say I have this variable: a = "amazingjob" and I want to slice the last three letters, but I want to do it backwards/using negative numbers, meaning: a[-1:-4] I want the result to be job 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
Top answer
1 of 9
47

Yes, calling s[0:-1] is exactly the same as calling s[:-1].

Using a negative number as an index in python returns the nth element from the right-hand side of the list (as opposed to the usual left-hand side).

so if you have a list as so:

myList = ['a', 'b', 'c', 'd', 'e']
print myList[-1] # prints 'e'

the print statement will print "e".

Once you understand that (which you may already, it's not entirely clear if that's one of the things you're confused about or not) we can start talking about slicing.

I'm going to assume you understand the basics of a slice along the lines of myList[2:4] (which will return ['c', 'd']) and jump straight into the slicing notation where one side is left blank.

As you suspected in your post, myList[:index] is exactly the same as myList[0:index].

This is also works the other way around, by the way... myList[index:] is the same as myList[index:len(myList)] and will return a list of all the elements from the list starting at index and going till the end (e.g. print myList[2:] will print ['c', 'd', 'e']).

As a third note, you can even do print myList[:] where no index is indicated, which will basically return a copy of the entire list (equivalent to myList[0:len(myList)], returns ['a', 'b', 'c', 'd', 'e']). This might be useful if you think myList is going to change at some point but you want to keep a copy of it in its current state.

If you're not already doing it I find just messing around in a Python interpreter a whole bunch a big help towards understanding these things. I recommend IPython.

2 of 9
19
>>> l = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz&']

# I want a string up to 'def' from 'vwx', all in between
# from 'vwx' so -2;to 'def' just before 'abc' so -9; backwards all so -1.
>>> l[-2:-9:-1]
['vwx', 'stu', 'pqr', 'mno', 'jkl', 'ghi', 'def']

# For the same 'vwx' 7 to 'def' just before 'abc' 0, backwards all -1
>>> l[7:0:-1]
['vwx', 'stu', 'pqr', 'mno', 'jkl', 'ghi', 'def']

Please do not become listless about list.

  1. Write the first element first. You can use positive or negative index for that. I am lazy so I use positive, one stroke less (below 7, or -3 for the start).
  2. Index of the element just before where you want to stop. Again, you can use positive or negative index for that (below 2 or -8 for stop).
  3. Here sign matters; of course - for backwards; value of stride you know. Stride is a 'vector' with both magnitude and direction (below -1, backwards all).

    l = [0,1,2,3,4,5,6,7,8,9]
    l[7:2:-1], l[-3:2:-1], [-3:-8:-1],l[7:-8:-1]
    

    All result in [7, 6, 5, 4, 3].

🌐
PREP INSTA
prepinsta.com › home › python tutorial › slicing with negative numbers in python
Slicing with Negative Numbers in Python | PrepInsta
September 9, 2023 - Let’s have a look at the python codes for slicing on the tuple. ... #Initialize the String String = tuple(['P', 'r', 'e', 'p', 'I', 'n', 's', 't', 'a']) #Slicing using 1 negative index arr = String[-2:] print(arr) #Slicing using 1 negative index arr = String[:-3] print(arr) #Slicing using 3rd negative index arr = String[::-1] print(arr) #Slicing using 2 negative indexes arr = String[-9:-4] print(arr)
🌐
EyeHunts
tutorial.eyehunts.com › home › negative slicing in python | example code
Negative slicing in Python | Example code - EyeHunts
October 21, 2021 - Using a negative number as an index in the slice method is called Negative slicing in Python. It returns the nth element from the right-hand..
🌐
Reddit
reddit.com › r/learnpython › list slicing with negative index
r/learnpython on Reddit: List slicing with negative index
December 15, 2021 -

Although I know how slicing works but following examples kind of stumped me.

values = [3,4,3,7,8,9,5]
values[:3:-1]    #o/p [5,9,8]
values[5:3:-1]    #o/p [9,8]

I always thought in list[start:stop:step], 'start' defaults to 0, 'stop' defaults to length-1 and 'step' defaults to 1. But this doesn't make sense here. What are the rules? Link to official docs will also be appreciated as I couldn't find that.

🌐
W3Schools
w3schools.com › python › gloss_python_string_negative_indexing.asp
Python String Negative Indexing
❮ Python Glossary · Use negative indexes to start the slice from the end of the string: Get the characters from position 5 to position 1, starting the count from the end of the string: b = "Hello, World!" print(b[-5:-2]) Try it Yourself » ...
Find elsewhere
🌐
Codecademy Forums
discuss.codecademy.com › get help › python
Negative slicing Python - Python - Codecademy Forums
May 10, 2020 - Why in negative slicing in my screenshot the last index value is not shown? Someone explain me. I am having some confusion on last index one. fav_fruit = "mango" print(fav_fruit[-3:-1]) here it prints as: ng why …
🌐
YouTube
youtube.com › caleb curry
Beginner Python Tutorial 25 - Negatives with String Slicing - YouTube
Guided video solutions for data structures, algorithms, and interview challenges - https://codebreakthrough.com. Use code YOUTUBE for 20% off your subscripti...
Published   April 21, 2020
Views   1K
🌐
CodeChef
codechef.com › learn › course › python-development › PYDEV09 › problems › PYTHPROB361C
Negative Slicing in Python for project building
Test your Learn Python for project building knowledge with our Negative Slicing practice problem. Dive into the world of python-development challenges at CodeChef.
🌐
Sentry
sentry.io › sentry answers › python › python slice notation
Python slice notation | Sentry
October 21, 2022 - In the example below, a string is sliced starting at index 2, stopping when the array is complete, and taking the values at every second step. num_string = "0123456" even_nums = num_string[2::2] print(even_nums) ... Slicing can use negative indexing.
🌐
LabEx
labex.io › tutorials › python-how-to-slice-lists-with-negative-indices-435398
How to slice lists with negative indices | LabEx
def safe_slice(lst, start=None, end=None): try: return lst[start:end] except IndexError: return "Invalid slice range" At LabEx, we emphasize mastering these slicing techniques for efficient Python programming. By mastering negative indices in Python list slicing, developers can write more concise and readable code.
🌐
TutorialsPoint
tutorialspoint.com › what-is-a-negative-indexing-in-python
What is a Negative Indexing in Python?
August 25, 2023 - Negative indexing in Python allows you to access elements from the end of a sequence (string, list, tuple) by using negative numbers. Instead of counting from the beginning (0, 1, 2...), negative indexing counts backwards from the last element (-1,
🌐
Hyperskill
hyperskill.org › university › python › slicing-in-python
Slicing in Python
July 22, 2024 - Slice notation in Python allows programmers to extract a portion of a sequence or a string using square brackets with one or two integers separated by a colon. Negative integers can count from the end of the sequence. Optional third arguments specify the step size.
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]
🌐
Codingem
codingem.com › home › negative indexing in python: a step-by-step guide (examples)
Negative Indexing in Python: A Step-by-Step Guide (Examples)
November 1, 2022 - Python indexing can be started from the end of the iterable. This is called negative indexing. -1 is the last value, -2 is the 2nd last.
🌐
Sololearn
sololearn.com › en › Discuss › 3353109 › howd-does-negative-and-positive-slicing-works-in-python
How'd does negative and positive slicing works in python?? | Sololearn: Learn to code for FREE!
March 15, 2026 - negative indices do not change the slicing direction. we can use pure postive indexing or pure negative indexing, as well as a mixture from both. the slicing direction is determined by the *step value* sample: txt = 'Python' 0 1 2 3 4 5 => positive ...
🌐
LabEx
labex.io › tutorials › python-how-to-use-negative-indexing-in-python-lists-418588
How to use negative indexing in Python lists | LabEx
Negative index slicing is memory-efficient · Creates a new list without modifying original · O(k) time complexity, where k is slice length · Check list length before slicing · Use try-except for robust code · Validate slice boundaries · ...