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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ what-is-negative-indexing-in-python
What is Negative Indexing in Python? - GeeksforGeeks
July 23, 2025 - Negative indexing in Python allows us to access elements from the end of a sequence like a list, tuple, or string.
Discussions

[2025 Day 4][Python] PSA: Python negative array indices will wrap around
I learned from AoC a few seasons ago that operating on a grid as a list of lists is frequently suboptimal. It can work, but you need to handle boundaries in some way, either by creating a buffer around your area of interest or explicitly checking indices at every iteration. I find that storing a grid as a set of tuples, where each element is the x-y coordinates of a single paper roll, works extremely well. Finding whether there's a roll at (x, y) is just (x, y) in locations. No boundary handling required. If you need more information about each location, then use a tuple-keyed dictionary instead. For example, I did some optimization on part 2 today by storing a the number of neighbors each roll has in a dictionary. More on reddit.com
๐ŸŒ r/adventofcode
49
147
December 4, 2025
Negative indexing in Python - Python - Data Science Dojo Discussions
Negative indexes refer to the positions of elements within an array-like object such as a list, tuple, or string, counting from the end of the data structure 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 ... More on discuss.datasciencedojo.com
๐ŸŒ discuss.datasciencedojo.com
1
0
November 9, 2022
Python numpy array negative indexing - Stack Overflow
[1:-1]: from the second (item/row) to the last (=second) item/row: that will lead to an empty array. ... Try first with a simple Python list of two elements: x = [1, 2], and index that in the same way: see what you get. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Negative indexing python code
If anyone can please explain how the code in red square exactly works. More on discuss.python.org
๐ŸŒ discuss.python.org
5
0
June 11, 2025
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-negative-indexing-in-python
What is negative indexing in Python?
For element 15, we can use the index -2 and so on. Note: Negative indexes start from index number -1, while positive indexes start from index number 0.
๐ŸŒ
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 - Python programming language supports ... last element, and -2 gives the second last element of an array. The negative indexing starts from where the array ends....
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_string_negative_indexing.asp
Python String Negative Indexing
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 ยป ยท Python Strings Tutorial ...
๐ŸŒ
Reddit
reddit.com โ€บ r/adventofcode โ€บ [2025 day 4][python] psa: python negative array indices will wrap around
r/adventofcode on Reddit: [2025 Day 4][Python] PSA: Python negative array indices will wrap around
December 4, 2025 - Agreed for python! Iโ€™m trying to make things work and then be increase perfโ€ฆ thanks for the answer :) ... Thatโ€™s exactly how I handled it! Negative indices donโ€™t matter because itโ€™s just not in the map and therefore not a paper roll! ... Thank you for this! ... This is how I do it as well! ... I have kind of a hybrid approach. My grid type is backed by a 1D array and is indexed by pairs of '(row .
๐ŸŒ
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,
Find elsewhere
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ user โ€บ basics.indexing.html
Indexing on ndarrays โ€” NumPy v2.6.dev0 Manual
As in Python, all indices are zero-based: for the i-th index \(n_i\), the valid range is \(0 \le n_i < d_i\) where \(d_i\) is the i-th element of the shape of the array. Negative indices are interpreted as counting from the end of the array (i.e., if \(n_i < 0\), it means \(n_i + d_i\)).
๐ŸŒ
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 of elements within an array-like object such as a list, tuple, or string, counting from the end of the data structure rather than the beginning.
๐ŸŒ
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 in a list and returns its index.
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.

๐ŸŒ
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 ...
๐ŸŒ
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.
๐ŸŒ
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 - Python is a powerful and versatile programming language that has many features and capabilities. One of these features is negative indexing, which allows you to access elements of a sequence (such as a list, a string, or a tuple) from the end, using negative numbers as indexes.
๐ŸŒ
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.
๐ŸŒ
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 - In Python, lists are zero-indexed, meaning that the first element is at index 0, the second element at index 1, and so on. Negative indexes, on the other hand, start from -1, where -1 represents the last element in the list, -2 represents the second-to-last element, and so forth.
๐ŸŒ
Execute Program
executeprogram.com โ€บ courses โ€บ python-for-programmers โ€บ lessons โ€บ negative-indexing
Python for Programmers: Negative Indexing
Learn programming languages like TypeScript, Python, JavaScript, SQL, and regular expressions. Interactive with real code examples.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ list โ€บ negative-indexing
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.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Negative indexing python code - Python Help - Discussions on Python.org
June 11, 2025 - If anyone can please explain how the code in red square exactly works.
๐ŸŒ
Omz Software
omz-software.com โ€บ pythonista โ€บ numpy โ€บ reference โ€บ arrays.indexing.html
Indexing โ€” NumPy v1.8 Manual
As in Python, all indices are zero-based: for the i-th index n_i, the valid range is 0 \le n_i < d_i where d_i is the i-th element of the shape of the array. Negative indices are interpreted as counting from the end of the array (i.e., if i < 0, it means n_i + i).