ShareYourGroupShareYourGroupPython

IndexError: list index out of range

An IndexError means you asked a list, tuple or string for a position it does not have. A list of three items has positions 0, 1 and 2. Asking for position 3 raises the error.

names = ["Asha", "Ravi", "Meena"]
print(names[3])

What Python says

Traceback (most recent call last):
  File "example.py", line 2, in <module>
    print(names[3])
          ~~~~~^^^
IndexError: list index out of range

Why it happens: counting from zero

Positions start at 0, so the last position is always one less than the length. This gap is the root of most index errors. Programmers call it an off-by-one mistake.

names = ["Asha", "Ravi", "Meena"]
print(len(names))
print(names[len(names) - 1])
print(names[-1])

Output

3
Meena
Meena

To get the last item, use -1. It works for any length and avoids the sum.

Cause 1: a loop that goes one step too far

marks = [70, 80, 90]
for i in range(len(marks) + 1):
    print(marks[i])

What Python says

70
80
90
Traceback (most recent call last):
  File "example.py", line 3, in <module>
    print(marks[i])
          ~~~~~^^^
IndexError: list index out of range

range(len(marks)) already gives exactly the valid positions. Adding 1 walks off the end. Better still, do not use positions at all:

marks = [70, 80, 90]
for m in marks:
    print(m)

Output

70
80
90

Looping over the items directly makes this error impossible. If you also need the position, use enumerate.

Cause 2: an empty list

results = []
print(results[0])

What Python says

Traceback (most recent call last):
  File "example.py", line 2, in <module>
    print(results[0])
          ~~~~~~~^^^
IndexError: list index out of range

An empty list has no position 0. This often happens when a search or a filter found nothing, or a file turned out to be empty. Check before you read.

results = []
if results:
    print(results[0])
else:
    print("Nothing found")

Output

Nothing found

An empty list counts as false in an if, so if results: means "if there is at least one item".

Cause 3: assigning to a position that does not exist

squares = []
squares[0] = 1

What Python says

Traceback (most recent call last):
  File "example.py", line 2, in <module>
    squares[0] = 1
    ~~~~~~~^^^
IndexError: list assignment index out of range

You cannot create a new position by assigning to it. Use append to make the list longer.

Cause 4: looking at the next item

temps = [30, 32, 31]
for i in range(len(temps)):
    if temps[i + 1] > temps[i]:
        print("warmer on day", i + 1)

What Python says

warmer on day 1
Traceback (most recent call last):
  File "example.py", line 3, in <module>
    if temps[i + 1] > temps[i]:
       ~~~~~^^^^^^^
IndexError: list index out of range

On the last round, i + 1 is beyond the end. When comparing neighbours, stop one early with range(len(temps) - 1).

Cause 5: popping from an empty list

stack = []
stack.pop()

What Python says

Traceback (most recent call last):
  File "example.py", line 2, in <module>
    stack.pop()
    ~~~~~~~~~^^
IndexError: pop from empty list

Slices never raise this error

A slice that reaches beyond the end simply gives what there is. This is handy for "the first five, or fewer if there are fewer".

names = ["Asha", "Ravi"]
print(names[:5])
print(names[5:])

Output

['Asha', 'Ravi']
[]

The same error with strings

word = "cat"
print(word[10])

What Python says

Traceback (most recent call last):
  File "example.py", line 2, in <module>
    print(word[10])
          ~~~~^^^^
IndexError: string index out of range

The message says "string index out of range", and the cause and the cure are the same.

How to find the cause

Just before the failing line, print the length of the list and the index you are about to use.

marks = [70, 80, 90]
i = 3
print(len(marks), i)

Output

3 3

An index equal to the length is one too many. Remember also that a negative index can go out of range: a list of three items accepts -1, -2 and -3, but not -4. For dictionaries, the matching error is KeyError.

Other error messages

Examples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.

This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.