Submitting the form below will ensure a prompt response from us.
One of the most common Python errors beginners and experienced developers encounter is:
IndexError: list index out of range
This error occurs when a program attempts to access a list element using an index that does not exist.
Understanding why this happens is essential for writing reliable Python code.
In Python, every list element has an index.
Example:
fruits = ["Apple", "Banana", "Orange"]
The indexes are:
| Value | Positive Index | Negative Index |
|---|---|---|
| Apple | 0 | -3 |
| Banana | 1 | -2 |
| Orange | 2 | -1 |
If you try to access an index beyond the available range, Python raises an IndexError.
Example:
fruits = ["Apple", "Banana", "Orange"]
print(fruits[3])
Output:
IndexError: list index out of range
Because index 3 does not exist.
Python lists are zero-indexed.
For a list containing 3 elements:
len(fruits)
returns:
3
Valid indexes are:
0, 1, 2
Attempting to access:
3
or higher causes an error.
Incorrect Code
numbers = [10, 20, 30]
print(numbers[5])
Result
IndexError: list index out of range
Fix
Check the list length first:
numbers = [10, 20, 30]
if len(numbers) > 2:
print(numbers[2])
Many indexing errors occur inside loops.
Problematic Example
items = ["A", "B", "C"]
for i in range(len(items) + 1):
print(items[i])
Error
The loop eventually tries:
items[3]
which does not exist.
Correct Version
items = ["A", "B", "C"]
for i in range(len(items)):
print(items[i])
Trying to access an element in an empty list immediately causes the error.
Example
data = []
print(data[0])
Output:
IndexError: list index out of range
Fix
Check whether the list contains elements.
data = []
if data:
print(data[0])
Lists generated from user input may contain fewer elements than expected.
Example
names = input("Enter names: ").split(",")
print(names[2])
If the user enters only one or two names, an IndexError occurs.
Safer Version
if len(names) > 2:
print(names[2])
else:
print("Not enough values")
Consider the list:
colors = [“Red”, “Blue”, “Green”]
| Positive Index | Negative Index | Value |
|---|---|---|
| 0 | -3 | Red |
| 1 | -2 | Blue |
| 2 | -1 | Green |
Valid indexes:
0, 1, 2
or
-1, -2, -3
Invalid indexes:
3, 4, -4
You Might Also Like:
Python allows graceful error handling using try-except.
Example
numbers = [1, 2, 3]
try:
print(numbers[5])
except IndexError:
print("Index does not exist")
Output:
Index does not exist
This prevents application crashes.
Suppose you process API data:
users = ["Alice", "Bob"]
print(users[2])
The API unexpectedly returns only two records.
A safer implementation:
users = ["Alice", "Bob"]
if len(users) > 2:
print(users[2])
else:
print("User not found")
Direct indexing raises errors:
letters = ["a", "b", "c"]
print(letters[5])
Error:
IndexError
However, slicing behaves differently:
print(letters[5:10])
Output:
[]
Slicing beyond bounds returns an empty list rather than raising an exception.
Incorrect:
items = ["x", "y", "z"]
print(items[len(items)])
Since:
len(items)
equals:
3
The highest valid index is:
2
Always verify list contents before accessing indexes.
This is especially common in loops:
for i in range(len(data) + 1):
The extra +1 often causes problems.
if len(my_list) > index:
Instead of:
for i in range(len(items)):
use:
for item in items:
try:
value = items[index]
except IndexError:
pass
Check API responses, files, and user input before indexing.
Avoid unnecessary index access when possible.
This:
for item in items:
print(item)
is often safer and more readable than:
for i in range(len(items)):
print(items[i])
Build Reliable Python Applications
Master error handling and best practices for production-ready code.
The “IndexError: list index out of range” exception occurs when Python attempts to access a list position that does not exist.
The most common causes include:
By validating list lengths, using proper loops, and implementing exception handling, you can prevent this error and create more reliable Python applications.
Understanding list indexing is a fundamental Python skill that every developer should master for effective debugging and error-free coding.