Python Tutoring Online
Home

Python List Index Out of Range Error

The IndexError: list index out of range is one of the most common errors beginners encounter when working with Python lists.

Need Help Learning Python?

Understanding Python errors is an important step in becoming a confident programmer.

If you are learning Python and need help debugging your code or understanding programming errors, one-to-one Python tutoring can help you progress faster.

Get Python Tutoring Help

This error occurs when a program tries to access an index that does not exist in a list.

Understanding List Indexes

Python lists use zero-based indexing. This means the first element of a list has index 0.

Python
numbers = [10,20,30]

print(numbers[0])
print(numbers[1])
print(numbers[2])

Output


10
20
30

Example of Index Out of Range Error

If we try to access an index that does not exist, Python will raise an error.

Python
numbers = [10,20,30]

print(numbers[3])

Error Output


IndexError: list index out of range

The list contains only three elements with indexes 0, 1, and 2. Index 3 does not exist.

Cause 1: Accessing Invalid Index

Python
colors = ["red","green","blue"]

print(colors[5])

Error


IndexError: list index out of range

Cause 2: Using Incorrect Loop Range

This error commonly occurs when loops exceed the list length.

Python
numbers = [10,20,30]

for i in range(5):
    print(numbers[i])

Error


IndexError: list index out of range

Correct Way to Loop Through Lists

Use the len() function to limit the loop range.

Python
numbers = [10,20,30]

for i in range(len(numbers)):
    print(numbers[i])

Output


10
20
30

Cause 3: Accessing Index After Removing Elements

If list elements are removed, the number of indexes changes.

Python
numbers = [10,20,30]

numbers.pop()

print(numbers[2])

Error


IndexError: list index out of range

Cause 4: Empty Lists

Trying to access elements in an empty list also causes this error.

Python
numbers = []

print(numbers[0])

Error


IndexError: list index out of range

How to Prevent This Error

You can prevent this error by checking the list length before accessing elements.

Python
numbers = [10,20,30]

if len(numbers) > 2:
    print(numbers[2])

Output


30

Using try–except to Handle Errors

Python allows programs to handle errors using try and except.

Python
numbers = [10,20,30]

try:
    print(numbers[5])
except IndexError:
    print("Index does not exist")

Output


Index does not exist

Common Situations Where This Error Occurs

Summary

The IndexError: list index out of range occurs when a program tries to access an index that does not exist in a list.

Using correct indexing, loops based on len(), and error handling techniques can prevent this problem.

Related Python List Tutorials




Stuck Learning Python?

Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.

Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.

    Get Python Tutoring

PythonTutor.net

Home    Tutorial     Online Live Trainings    Contact Us           © 2026 All rights reserved by www.PythonTutor.net