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.

Answer from Redwood on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › slicing-with-negative-numbers-in-python
Slicing with Negative Numbers in Python - GeeksforGeeks
July 16, 2026 - 2. Using slice(): Python provides slice() function to achieve the same result in a more explicit way. ... Example: Here we use the slice() function to achieve the same slicing as before. ... text = "DataScience" left = text[slice(-7)] middle = text[slice(-7, -3)] right = text[slice(-3, None)] print(left) print(middle) print(right) ... In this code, we create a list called items. Using negative slicing, we extract last four elements as vegetables and two elements before them as fruits.
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].

Discussions

List slicing with negative index
On negative step, the default start and stop are reversed. Start is len-1 stop is 0. 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
List negative slicing
Because your upper bound is lower than your higher bound. Negative indexing means you work through the list backwards, so my_list[-1:-2] means you want from the last element to the first element but still going in order. Try my_list[-2:-1] or my_list[-1:-2:-1] and see which one suits you better More on reddit.com
🌐 r/learnpython
2
1
April 21, 2020
🌐
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…
🌐
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..
🌐
CodeChef
codechef.com › learn › course › python-development › PYDEV09 › problems › PYTHPROB361C
Negative Slicing in Python for project building
How It Works • Negative indices start at -1 for the last character, -2 for the second-to-last, and so on. • A slice using negative indices follows the same string[start:end] pattern, except both start and end can be negative.
🌐
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.

Find elsewhere
🌐
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 » ...
🌐
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 …
🌐
Knowledgehills
knowledgehills.com › python › negative-indexing-slicing-stepping-comparing-lists.htm
Python Lists – Negative Indexing, Slicing, Stepping, Comparing, Max and Min – Knowledge Hills
lst = ['Ajay', 'Bobby','Ashok', 'Vijay', 'Anil', 'Rahul','Alex', 'Christopher'] print (lst[1:]) print (lst[0:]) print (lst[2:-2]) # all elements starting from third element but skips the last two elements. print (lst[::2]) # this will print all alternate elements (begin to end in steps of 2) print (lst[::-1]) # this will print all elements in reverse order ... The first print command will skip the first element and print the entire list whereas the second print command to print lst1[0:] will print the entire list as it is. The third print combines slicing with a negative index.
🌐
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.
🌐
EITCA
eitca.org › home › how do negative indexes work in python when accessing elements in a list?
How do negative indexes work in Python when accessing elements in a list? - EITCA Academy
August 3, 2023 - Slicing with negative indexes works in the same way as slicing with positive indexes, but the start and end points are specified relative to the end of the list. For example, if we want to extract the last three elements of the list, we can use the slice -3: (which is equivalent to [3, 4, 5]): ...
🌐
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.
🌐
LabEx
labex.io › tutorials › python-how-to-slice-lists-with-negative-indices-435398
How to slice lists with negative indices | LabEx
This tutorial explores the fundamental ... list elements in Python programming. In Python, negative indices provide a powerful way to access list elements from the end of the list....
🌐
Reddit
reddit.com › r/learnpython › list negative slicing
r/learnpython on Reddit: List negative slicing
April 21, 2020 -

How negative slicing works?

Here vals is taking values: 3 which coresponds to -1 index and 2 which coresponds to -2 index.

But python answer is [ ], wondering why?

nums=[1,2,3]
vals=nums[-1:-2]
print(vals)
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]
🌐
Medium
medium.com › @wordpediax › python-slicing-complete-guide-explained-a6eaaa7dfaf5
Python Slicing: Complete Guide Explained | by wordpediax | Medium
October 1, 2025 - Explanation: Start = 4 and Stop = 1. Since Stop = -3; therefore negative indexing is applied. At index 4 character ‘O’ is stored and Stop does not include the last index; therefore, not any other character left in range to slice. Now it’s Your Turn! ... Perform the Python slice on the above String.
🌐
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 ...