← Back to Error Guide
IndexError

How to Fix IndexError

Understanding and resolving IndexError in Python

What is IndexError?

An IndexError occurs when you try to access an index that is out of the range of a list or other indexable collection.

Common Causes

Common causes of this error...

How to Fix

Wrong Code

# Code that causes the error
my_list = [1, 2, 3]
print(my_list[3])

Correct Code

# Fixed code
my_list = [1, 2, 3]
if len(my_list) > 3:
    print(my_list[3])
else:
    print('Index out of range')

More Python Error Guides