Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Positive and negative indices in Python?
Python sequences like lists, tuples, and strings support two types of indexing: positive indexing (starting from 0) and negative indexing (starting from -1). This tutorial explains both approaches with practical examples.
What Are Sequence Indexes?
Indexing allows us to access individual elements in Python sequence data types. There are two types:
Positive indexing Starts from 0 and increases to n-1 (where n is the total number of elements)
Negative indexing Starts from -1 (last element) and moves backwards to -n
Positive Indexing Examples
Positive indexing starts from 0 for the first element ?
# Accessing elements with positive indices
numbers = [10, 20, 30, 40, 50]
print("First element:", numbers[0])
print("Second element:", numbers[1])
print("Third element:", numbers[2])
First element: 10 Second element: 20 Third element: 30
Index Out of Range Error
Accessing an index that doesn't exist raises an IndexError ?
numbers = [10, 20, 30, 40, 50]
try:
print(numbers[10])
except IndexError as e:
print("Error:", e)
Error: list index out of range
Negative Indexing Examples
Negative indexing starts from -1 for the last element ?
# Accessing elements with negative indices
numbers = [10, 20, 30, 40, 50]
print("Last element:", numbers[-1])
print("Second last:", numbers[-2])
print("Third last:", numbers[-3])
Last element: 50 Second last: 40 Third last: 30
Negative Indexing with Slicing
You can combine negative indices with slicing operations ?
numbers = [10, 20, 30, 40, 50]
# Get last three elements
print("Last 3 elements:", numbers[-3:])
# Get elements from index -4 to -1 (excluding -1)
print("From -4 to -2:", numbers[-4:-1])
Last 3 elements: [30, 40, 50] From -4 to -2: [20, 30, 40]
Practical Applications
Removing Last Element
Use negative indexing with pop() to remove elements from the end ?
fruits = ['apple', 'banana', 'cherry', 'date']
print("Original list:", fruits)
# Remove last element
removed = fruits.pop(-1)
print("Removed:", removed)
print("Updated list:", fruits)
Original list: ['apple', 'banana', 'cherry', 'date'] Removed: date Updated list: ['apple', 'banana', 'cherry']
Finding Element Index
Use the index() method to find the position of an element ?
colors = ['red', 'green', 'blue', 'yellow', 'purple']
print("Index of 'blue':", colors.index('blue'))
print("Index of 'yellow':", colors.index('yellow'))
# Access using found index
blue_index = colors.index('blue')
print("Element at index", blue_index, "is:", colors[blue_index])
Index of 'blue': 2 Index of 'yellow': 3 Element at index 2 is: blue
Comparison of Indexing Methods
| Method | Starting Point | Direction | Best For |
|---|---|---|---|
| Positive | 0 (first element) | Left to right | Accessing from beginning |
| Negative | -1 (last element) | Right to left | Accessing from end |
Conclusion
Python's dual indexing system provides flexibility for accessing sequence elements. Use positive indexing (0, 1, 2...) when working from the beginning and negative indexing (-1, -2, -3...) when accessing elements from the end. This feature makes Python code more readable and efficient for common operations like getting the last element or removing items from the end.
