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
🌐
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 » ...
🌐
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. Syntax: slice(start, stop, step) Parameters: start: Starting index (can be None or negative). stop: Ending index (exclusive). step: Step value (negative step reverses the sequence).
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 zero (-0) as a slice index - Ideas - Discussions on Python.org
While using a negative number as a slice index from the end of a sequence is quite a stroke of genius, it hits its limit when one tries to represent a slice beyond the end of a sequence. In Python multiple assignment: w… More on discuss.python.org
🌐 discuss.python.org
1
March 5, 2025
Negative Slicing in Python
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:-2] print (vals) Thank you in advance. More on discuss.python.org
🌐 discuss.python.org
3
0
September 8, 2023
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
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].

🌐
LabEx
labex.io › tutorials › python-how-to-slice-lists-with-negative-indices-435398
How to slice lists with negative indices | LabEx
In Python, list slicing follows the syntax: list[start:end:step] graph LR A[Start Index] --> B[End Index] B --> C[Step Value] ## Sample list numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ## Basic slicing print(numbers[2:7]) ## [2, 3, 4, 5, 6] print(numbers[:4]) ## [0, 1, 2, 3] print(numbers[6:]) ## [6, 7, 8, 9] ## Reverse partial list print(numbers[-5:-1]) ## [5, 6, 7, 8] ## Slice from end print(numbers[-3:]) ## [7, 8, 9]
🌐
Knowledgehills
knowledgehills.com › python › negative-indexing-slicing-stepping-comparing-lists.htm
Python Lists – Negative Indexing, Slicing, Stepping, Comparing, Max and Min – Knowledge Hills
Well, the answer is “Alex”, this is because when you give a negative index number Python counts the element from the right. The rightmost element is at the index of -1. Few other programming languages have this negative index, but this feature is extremely useful.
🌐
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.

🌐
TutorialsPoint
tutorialspoint.com › what-is-a-negative-indexing-in-python
What is a Negative Indexing in Python?
August 25, 2023 - # Basic slicing syntax sequence[start:stop:step] # Examples with negative indexing sequence[-4:-1] # From 4th last to 2nd last sequence[-5:] # From 5th last to end sequence[:-2] # From beginning to 3rd last sequence[::-1] # Reverse entire sequence · You can access individual elements using negative indices ? text = "Python" print("Last character:", text[-1]) print("Second last character:", text[-2]) print("Third last character:", text[-3])
Find elsewhere
🌐
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)
🌐
Python.org
discuss.python.org › ideas
Negative zero (-0) as a slice index - Ideas - Discussions on Python.org
March 5, 2025 - While using a negative number as a slice index from the end of a sequence is quite a stroke of genius, it hits its limit when one tries to represent a slice beyond the end of a sequence.
🌐
EyeHunts
tutorial.eyehunts.com › home › negative slicing in python | example code
Negative slicing in Python | Example code - EyeHunts
October 21, 2021 - Python supports using negative numbers to index into a string: -1 means the last char, -2 is the next to last, and so on. Simple example code. ... Calling s[0:-1] is exactly the same as calling s[:-1]. myList = ['A', 'B', 'C', 'D', 'E'] ...
🌐
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]): ...
🌐
LabEx
labex.io › tutorials › python-how-to-slice-sequences-with-negative-index-431287
How to slice sequences with negative index | LabEx
In LabEx's advanced Python courses, mastering these slicing techniques can significantly enhance your data manipulation skills. def safe_slice(sequence, start=None, end=None, step=None): try: return sequence[start:end:step] except Exception as e: print(f"Slicing error: {e}") return None ## Robust slicing implementation sample_list = [1, 2, 3, 4, 5] result = safe_slice(sample_list, start=1, end=-1, step=2) print(result) ## Output: [2, 4] By mastering negative index slicing in Python, developers can write more concise and readable code when working with sequences.
🌐
Wordaligned
wordaligned.org › articles › negative-sequence-indices-in-python
Negative Sequence Indices in Python
August 1, 2016 - Omitting an index defaults it to the end of the sequence. Omit both indices and both ends of the sequence are defaulted, giving a sliced copy.
🌐
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…
🌐
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 …
🌐
Cse163
cse163.github.io › book › module-1-introduction-to-python › lesson-3-strings-and-lists › negative-indices.html
Negative Indices — Intermediate Data Programming
Asking Python to go ” n before ... uses negative numbers! The idea is you start at the last character ( 'd' ) being at index -1 (since it is at index len(s) - 1 in our indexing scheme), the second to last ( 'l' ) being -2 , etc....
Top answer
1 of 3
6

areas[-4:0] translates to areas[len(areas) - 4: 0], which is effectively slicing from a higher index to a lower. Semantically, this doesn't make much sense, and the result is an empty list.

You're instead looking for:

>>> areas[-4:]
['bedroom', 10.75, 'bathroom', 9.5]

When the last index is not specified, it is assumed you slice till the very end.


As an aside, specifying 0 would make sense when you slice in reverse. For example,

>>> areas[-4:0:-1]
['bedroom', 20.0, 'living room', 18.0, 'kitchen', 11.25]

Happens to be perfectly valid. Here, you slice from len(areas) - 4 down to (but not including) index 0, in reverse.

2 of 3
1

0 is not a negative number which is why it will always refer to the left-most element.

If you are hardcoding a single slice that is no problem, because you can just leave out the right boundary as in areas[-4:]

But what to do if your boundaries are computed at runtime?

>>> for left in range(-8, -3, 2):
...     right = left + 4
...     print(areas[left:right])
... 
['kitchen', 18.0, 'living room', 20.0]
['living room', 20.0, 'bedroom', 10.75]
[]

As you found out this doesn't work.

You'll often hear to just add the length of the list:

>>> for left in range(-8, -3, 2):
...     right = left + 4
...     print(areas[len(areas)+left:len(areas)+right])
... 
['kitchen', 18.0, 'living room', 20.0]
['living room', 20.0, 'bedroom', 10.75]
['bedroom', 10.75, 'bathroom', 9.5]

But that doesn't always work either:

>>> for left in range(-12, -3, 2):
...     right = left + 4
...     print(areas[len(areas)+left:len(areas)+right])
... 
[]
['hallway', 11.25, 'kitchen', 18.0]
['kitchen', 18.0, 'living room', 20.0]
['living room', 20.0, 'bedroom', 10.75]
['bedroom', 10.75, 'bathroom', 9.5]

So here is an idiom that works in a few more cases:

>>> for left in range(-12, -3, 2):
...     right = left + 4
...     print(areas[left or None:right or None])
... 
['hallway', 11.25]
['hallway', 11.25, 'kitchen', 18.0]
['kitchen', 18.0, 'living room', 20.0]
['living room', 20.0, 'bedroom', 10.75]
['bedroom', 10.75, 'bathroom', 9.5]

But you can break this as well:

>>> for left in range(-12, -1, 2):
...     right = left + 4
...     print(areas[left or None:right or None])
... 
['hallway', 11.25]
['hallway', 11.25, 'kitchen', 18.0]
['kitchen', 18.0, 'living room', 20.0]
['living room', 20.0, 'bedroom', 10.75]
['bedroom', 10.75, 'bathroom', 9.5]
[]

What do we learn from this? Negative indices are ok for hard coding but require some care when used dynamically. In a program, it may be safest to avoid negative semantics and consistently use max(0, index).