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
🌐
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.

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

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
python - What is the use case for negative slicing and indexing in lists? - Stack Overflow
Referring to 30 Python Language features 1.6 List slices with negative indexing: >>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> a[-4:-2] [7, 8] Where is negative slicing and More on stackoverflow.com
🌐 stackoverflow.com
list - Negative Indexing & Slicing Python - Stack Overflow
Long time reader first time poster, ever. I am trying to understand all I can about indexing, slicing, negative indexing, etc. with lists. If I have a list: toeFriendly_terms=['feet', 'shoes', ' More on stackoverflow.com
🌐 stackoverflow.com
Negative Indexes in Lists
if you would want the last 3 items without knowing the length of the list. [-3:] More on reddit.com
🌐 r/learnpython
25
5
October 26, 2024
🌐
GeeksforGeeks
geeksforgeeks.org › python › slicing-with-negative-numbers-in-python
Slicing with Negative Numbers in Python - GeeksforGeeks
July 16, 2026 - slice(-7, -3) extracts the characters from index -7 to -3 (excluding -3), giving "Scie". slice(-3, None) extracts the last three characters of the string, giving "nce". 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. Python ·
🌐
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.
🌐
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]): ...
🌐
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)
🌐
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] ...
Find elsewhere
🌐
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 side of the list (as opposed to the usual left-hand side).
🌐
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 ...
🌐
Toppr
toppr.com › guides › computer-science › programming-with-python › list-operations › slicing-list
Python Slicing List: Negative Index List Slicing, IndexJump, FAQs
May 24, 2021 - Initial List: ['T', 'O', 'P', 'P', ... Elements sliced from index -6 to -1 ['R', 'T', 'O', 'P', 'P'] Printing List in reverse: ['R', 'P', 'P', 'O', 'T', 'R', 'O', 'F', 'R', 'P', 'P', 'O', 'T'] Slicing list is certainly a popular practice in Python whose use can take place with both positive indexes and negative ...
🌐
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…
🌐
LabEx
labex.io › tutorials › python-how-to-slice-sequences-with-negative-index-431287
How to slice sequences with negative index | LabEx
## Reversing sequences using negative step fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry'] ## Full reverse print(fruits[::-1]) ## Output: ['elderberry', 'date', 'cherry', 'banana', 'apple'] ## Reverse with partial selection print(fruits[-3::-1]) ## Output: ['date', 'cherry', 'banana', 'apple'] graph LR A[Original Sequence] --> B[Start Index] B --> C[End Index] C --> D[Step Value] D --> E[Resulting Slice] ## Complex slicing examples text = "LabEx Python Programming" ## Extract every second character print(text[::2]) ## Output: "Lb yhnPormn" ## Extract last four characters print(text[-4:]) ## Output: "ming" ## Extract from beginning to specific point print(text[:-5]) ## Output: "LabEx Python Prog"
🌐
TutorialKart
tutorialkart.com › python › how-to-slice-a-list-with-negative-indices-in-python
How to Slice a List with Negative Indices in Python
February 10, 2025 - In Python, you can slice a list using negative indices to access elements from the end of the list. The syntax list works with negative indices, where -1
🌐
W3Schools
w3schools.com › python › gloss_python_string_negative_indexing.asp
Python String Negative Indexing
Remove List Duplicates Reverse ... Interview Q&A Python Training · ❮ 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 ...
🌐
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 - Negative slicing also supports negative step size. This makes the slicing go backward. For example, let’s reverse the list of names. To do this, you do not need the start and stop parameters, as you start from the beginning and stop at the end: names = ["Alice", "Bob", "Charlie", "David", "Emmanuel", "Fiona"] last_three = names[::-1] print(last_three) ... Today you learned how to start indexing from the end of an iterable in Python.
🌐
Stack Overflow
stackoverflow.com › questions › 59304244 › negative-indexing-slicing-python
list - Negative Indexing & Slicing Python - Stack Overflow
0 List without negative indexing · 4 Slicing to reverse part of a list in python · 4 How can I slice a list till end with negative indexes · 9 Convert negative index in Python to positive index · 1 List in Python avoid only the first negative element · 0 Set negative indices in list to zero ·
🌐
Reddit
reddit.com › r/learnpython › negative indexes in lists
r/learnpython on Reddit: Negative Indexes in Lists
October 26, 2024 -

I see how useful using the -1 index can be for lists but is there any world where you’d actually need to use a -3 index (in a list of 4) etc. instead of the zero index? I’m new to coding so I want to learn as much as possible but I’m not sure of any case (as of right now) where this would be necessary.

🌐
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. >>> domain[-3:] 'org' >>> domain[:4] 'word' >>> digits[:] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] I prefer the list(digits) form for copying digits but you should certainly be familiar with the digits[:] version.
🌐
AlgoCademy
algocademy.com › link
Negative Index in Python | AlgoCademy
This code demonstrates how to access elements, iterate in reverse, and slice lists using negative indexes. When working with negative indexing, debugging and testing are crucial: Debugging: Use print statements to check the values of negative indexes and ensure they are within the valid range. Testing: Write test cases to verify the correctness of functions that use negative indexing. Use Python's unittest module for structured testing.