Code with the error

days = ["Mon", "Tue", "Wed"]
print(days[3])

Error message (Python 3.12, last line)

IndexError: list index out of range

Corrected code

days = ["Mon", "Tue", "Wed"]
print(days[len(days) - 1])

Output

Wed

Common causes

  • Counting from 1 instead of 0.
  • Using len(items) as an index.
  • Taking an item from an empty list.
  • A loop goes one step too far, for example with items[i + 1].

Practise in the course

Lesson: Lists

Frequently asked questions

How do I get the last item safely?

Use items[-1]. It works for any list that is not empty.

Does slicing raise IndexError?

No. A slice such as items[0:10] simply returns the items that exist.