Negative numbers mean that you count from the right instead of the left. So, list[-1] refers to the last element, list[-2] is the second-last, and so on.

Answer from Toomai on Stack Overflow
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-negative-indexing-in-python
What is negative indexing in Python?
Line 9: We get the ending negative index of the list using the formula (sizeOfList * -1) - 1. We convert the sizeOfList variable to a negative number (-5) by multiplying it by -1 and adding -1 to iterate till -6 because -6 is not included in ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-negative-index-of-element-in-list
Python - Negative index of Element in List - GeeksforGeeks
January 29, 2025 - For example, we are having a list li = [10, 20, 30, 40, 50] and the given element is 30 we need to fin the negative index of it so that given output should be -3. index() method in Python searches for the first occurrence of a specified element ...
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ negative indexing in python list โ€“ how to use โ€œ-1โ€ parameter
Negative Indexing in Python List - How to Use "-1" Parameter - AskPython
April 4, 2023 - The process of indexing from the opposite end is called Negative Indexing. In negative Indexing, the last element is represented by -1. ... We have a built-in function reverse() in Python to reverse a list, letโ€™s take a look.
๐ŸŒ
Data Science Dojo
discuss.datasciencedojo.com โ€บ python
Negative indexing in Python - Python - Data Science Dojo Discussions
November 9, 2022 - Negative indexes refer to the positions ... rather than the beginning. For example, in a list with 5 elements, the last element can be accessed using the index -1, the second to ......
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_string_negative_indexing.asp
Python String Negative Indexing
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... Python Strings Tutorial String Literals Assigning a String to a Variable Multiline Strings Strings are Arrays Slicing a String String Length Check In String Format String Escape Characters
๐ŸŒ
Quora
quora.com โ€บ What-is-negative-index-in-Python
What is negative index in Python? - Quora
Answer (1 of 24): Negative index is a useful concept allowing you to easily index an array (or list in Pythonโ€™s case) relative from the end rather than the beginning. The logic of negative index is very simple and straightforward. Imagine that youโ€™ve got an array of length n. That is to ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ what-is-negative-indexing-in-python
What is Negative Indexing in Python? - GeeksforGeeks
July 23, 2025 - This feature is unique to Python and makes it easier to work with data structures when we need to retrieve elements starting from the end. In this article, we will explore negative indexing, how it works and its practical applications.
Find elsewhere
๐ŸŒ
i2tutorials
i2tutorials.com โ€บ home โ€บ blogs โ€บ what are negative indexes and why are they used?
What are negative indexes and why are they used? | i2tutorials
January 13, 2022 - The negative indexing starts from where the array ends. This means that the last element of the array is the first element in the negative indexing which is -1. Example: arr = [10, 20, 30, 40, 50] print (arr[-1]) print (arr[-2])
Top answer
1 of 2
11

You have this statement:

In [31]: x[0:-1]

This way of indexing means that "start at 1st row and go till the last row (excluded)". That's why we get the first row as a result.

Out[31]: array([[0, 1, 2, 3, 4]])

But, when you do:

 In [31]: x[1:-1]   
 Out[31]: array([], shape=(0, 5), dtype=int64)

It's asking NumPy to "start at second row and not include the last row". Since here the second row is also the last row, it is excluded and we get an empty array as a result.


More information: There's nothing specific about using negative indexing such as -1 here. For instance, the following ways of indexing would also return empty arrays.

# asking to "start at first row and end at first row"
In [42]: x[0:0]  
Out[42]: array([], shape=(0, 5), dtype=int64)

# asking to "start at second row and end at second row"
In [43]: x[1:1]  
Out[43]: array([], shape=(0, 5), dtype=int64)

When it comes to indexing in Python/NumPy, it's always "left inclusive and right exclusive".

Here's something in plain Python (i.e. indexing a list)

In [52]: lst = [1, 2] 

In [53]: lst[1:-1]    
Out[53]: []   # an empty list

Please note the construct of indexing which is: [start:stop:step]

If we start and stop at the same index, then we get nowhere and an empty data structure (array/list/tuple etc.) is returned as a result.

2 of 2
2

If you request a slice x[a:b], you will receive a section spanning from a up to but not including b. So if you slice x[1:-1], the resulting array will not include -1, which happens to be the same as 1 in a (2,5) array. Another example:

>>> import numpy as np
>>> x = np.arange(15)
>>> x.shape = (3,5)
>>> x
array([[0,  1,  2,  3,  4],
       [5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
>>> x[0:-1]
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
>>> x[1:-1]
array([[5, 6, 7, 8, 9]])

The last operation above slices x from row 1 up to (not including) the last row, which is just row 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 - For instance, letโ€™s get the last three names from the list: names = ["Alice", "Bob", "Charlie", "David", "Emmanuel", "Fiona"] last_three = names[-3:] print(last_three) ... Negative slicing also supports negative step size.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ what-is-a-negative-indexing-in-python
What is a Negative Indexing in Python?
If the values above are in negative, ... # Display the String print("String = ", myStr) # Slice the string # Negative Indexing print("String after slicing (negative indexing) = ", myStr[-4:-1])...
๐ŸŒ
DEV Community
dev.to โ€บ hichem-mg โ€บ negative-indexing-in-python-with-examples-1ind
Negative Indexing in Python, with Examples ๐Ÿ - DEV Community
June 9, 2024 - In this tutorial, I will go through what negative indexing is, how it works, and its practical applications in Python programming.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ negative index of list in python
Negative Index of List in Python - Spark By {Examples}
May 31, 2024 - Inside the loop, you check each element of the list to see if it matches the target element element. If you find a match, you calculate the negative index of the element using the formula -len(my_list) + i, where i is the index of the element.
๐ŸŒ
Medium
medium.com โ€บ @journalehsan โ€บ what-is-negative-indexing-in-python-and-how-to-use-it-34ec7ac5b36
What is Negative Indexing in Python and How to Use It? ๐Ÿ | by Ehsan Tork | Medium
June 9, 2023 - The answer is simple: Python adds ... For example, if you have a list with four elements and you use an index of -1, Python will add 4 (the length of the list) to -1 (the index) and get 3 (the positive 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]):
๐ŸŒ
Career Ride
careerride.com โ€บ python-negative-index.aspx
What is a negative index in python?
What is a negative index in python? - Python arrays & list items can be accessed with positive or negative numbers (also known as index)........
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ understanding-python-numpy-array-negative-indexing-a-comprehensive-guide
Understanding Python NumPy Array Negative Indexing: A Guide | Saturn Cloud Blog
October 4, 2023 - In this example, arr[-2:] returns a new array with the last two elements of the original array. Negative indexing is a powerful feature in Pythonโ€™s NumPy library that allows you to access elements from the end of an array.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ numpy โ€บ array-indexing
Numpy Array Indexing (With Examples)
In the above example, we have modified elements of the numbers array using array indexing. numbers[0] = 12 - modifies the first element of numbers and sets its value to 12 ยท numbers[2] = 14 - modifies the third element of numbers and sets its value to 14 ยท NumPy allows negative indexing for ...