VOOZH about

URL: https://www.geeksforgeeks.org/python/what-is-negative-indexing-in-python/

⇱ What is Negative Indexing in Python? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

What is Negative Indexing in Python?

Last Updated : 23 Jul, 2025

Negative indexing in Python allows us to access elements from the end of a sequence like a list, tuple, or string. 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.

Understanding Negative Indexing

In Python, sequences have both positive and negative indices:

  • Positive Indexing: Starts from 0 and goes up to n-1 (where n is the length of the sequence).
  • Negative Indexing: Starts from -1 for the last element and goes up to -n for the first element.

Output
50
40

Negative Indexing with Strings

Strings in Python are also sequences so we can use negative indexing to access characters from the end of the string.


Output
n
h

Let's see some more examples of Negative Indexing

Accessing the Last Few Elements

Negative indexing is especially useful when we need to access elements at the end of a sequence without knowing its length.


Output
[20, 25]

Reversing a Sequence

Negative indexing can be combined with slicing to reverse a sequence.


Output
[5, 4, 3, 2, 1]
nohtyP

Negative Indexing in Nested Structures

Negative indexing also works for nested lists or tuples.


Output
[5, 6]
6

Common Mistakes to Avoid - Index Out of Range

Using a negative index that exceeds the length of the sequence will raise an IndexError.


Comment
Article Tags:
Article Tags: