Write Python. Understand every error.

Open the editor

Error guide · IndexError

IndexError: list index out of range

You asked for a position in a list (or string) that does not exist.

Example

marks = [72, 85, 90]
print(marks[3])

What Python says

IndexError: list index out of range

As printed by Python 3.12. Other versions may word it slightly differently.

Try this code in Py-CRAFT

Why it happens

Indexes start at 0, so a list of 3 items has positions 0, 1 and 2. Position 3 is past the end. This often happens in loops that go one step too far.

How to fix it

marks = [72, 85, 90]
print(marks[2])

Output: 90

Tips

Learn the ideas behind it

Other common errors

Share this page: