Negative indexing in Python is used to access elements from the end of a sequence.
- -1 represents the last element.
- -2 represents the second-last element.
- Negative indexing works with sequences such as lists, strings and tuples.
Example
Consider the following list:
[10, 20, 30, 40, 50]
The indexes are:
Positive: 0 1 2 3 4
10 20 30 40 50
Negative: -5 -4 -3 -2 -1
10 20 30 40 50
Accessing Via Negative Indexing
Elements can be accessed using a negative index inside square brackets.
numbers = [10, 20, 30, 40, 50]
print(numbers[-1])
print(numbers[-2])
print(numbers[-5])
Output
50 40 10
Explanation:
- numbers[-1] returns the last element.
- numbers[-2] returns the second-last element.
- numbers[-5] returns the first element.
- The index moves toward the beginning as the negative value decreases.
Negative Indexing with Lists
Lists support negative indexing, making it easy to access elements near the end.
fruits = ["Apple", "Banana", "Mango", "Orange"]
print(fruits[-1])
print(fruits[-3])
Output
Orange Banana
Explanation:
- fruits[-1] accesses Orange, the last element.
- fruits[-3] accesses Banana, the third element from the end.
- Length of the list does not need to be calculated.
Negative Indexing with Strings
It can also be used to access characters from the end, of a string.
text = "Python"
print(text[-1])
print(text[-2])
print(text[-6])
Output
n o P
Explanation:
- text[-1] returns the last character.
- text[-2] returns the second-last character.
- text[-6] returns the first character.
Negative Indexing with Tuples
Tuples also support negative indexing because they are ordered sequences.
numbers = (10, 20, 30, 40, 50)
print(numbers[-1])
print(numbers[-3])
Output
50 30
Explanation:
- numbers[-1] returns the last element.
- numbers[-3] returns the third element from the end.
- Indexing rule is the same as that of lists.
Negative Indexing in Nested Sequences
It can be applied at multiple levels when working with nested sequences.
data = [[10, 20], [30, 40], [50, 60]]
print(data[-1])
print(data[-1][-1])
Output
[50, 60] 60
Explanation:
- data[-1] accesses the last nested list.
- data[-1][-1] accesses the last element of that nested list.
- Multiple indexes can be combined to reach nested elements.
Modifying Elements Using Negative Indexing
Negative indexing can also be used to modify elements of mutable sequences such as lists.
numbers = [10, 20, 30, 40]
numbers[-1] = 100
print(numbers)
Output
[10, 20, 30, 100]
Explanation:
- numbers[-1] refers to the last element.
- Assigning a new value replaces the existing element.
- This works with lists because they are mutable.
Invalid Negative Index
A negative index must fall within the valid range of sequence. Otherwise, Python raises an IndexError.
numbers = [10, 20, 30]
print(numbers[-4])
Output
IndexError: List index out of range
Explanation:
- List contains three elements.
- Its valid negative indexes are -1, -2 and -3.
- -4 is outside the valid range.
Negative Indexing vs Positive Indexing
| Positive Indexing | Negative Indexing |
|---|---|
| Starts from 0 | Starts from -1 |
| Moves from beginning to end | Moves from end to beginning |
| First element is at index 0 | Last element is at index -1 |
| Example: numbers[0] | Example: numbers[-1] |