Python errors / IndexError

IndexError: list index out of range

A position was asked for that the list or string doesn't have.

IndexError: list index out of range

Check these first

  1. Positions start at 0. A list of 3 items has positions 0, 1 and 2. The last one is len(items) - 1, or simply items[-1].
  2. Is it empty? if items: is true only when the list has something in it.
  3. Print the length. print(len(items), i) just before the failing line shows how far off the position is.

Causes and fixes

1 Positions start at 0

The first item is at position 0, so in a list of three the last is at 2, not 3.

Broken
colors = ["red", "green", "blue"]
print(colors[3])
IndexError: list index out of range
Fixed
colors = ["red", "green", "blue"]
print(colors[2])
blue

2 The list is empty

An empty list has no position 0. Check that it has items before reading one.

Broken
scores = []
print(scores[0])
IndexError: list index out of range
Fixed
scores = []
if scores:
    print(scores[0])
else:
    print("No scores yet")
No scores yet

3 A loop that goes one too far

A loop that looks at the next item, items[i + 1], has to stop one position before the end.

Broken
nums = [10, 20, 30]
for i in range(len(nums)):
    print(nums[i + 1] - nums[i])
IndexError: list index out of range
Fixed
nums = [10, 20, 30]
for i in range(len(nums) - 1):
    print(nums[i + 1] - nums[i])
10
10

4 A position in a string

Strings count their letters the same way: the first is at 0 and the last is at len(word) - 1, or word[-1].

Broken
word = "cat"
print(word[3])
IndexError: string index out of range
Fixed
word = "cat"
print(word[-1])
t

5 pop() from an empty list

pop() takes the last item out, so the list needs one. Check first, especially in a loop that keeps popping.

Broken
stack = []
print(stack.pop())
IndexError: pop from empty list
Fixed
stack = []
if stack:
    print(stack.pop())
else:
    print("Nothing to pop")
Nothing to pop

Other messages

More messages IndexError can show. Open yours for what it means and how to fix it.

list assignment index out of range

You can't put a value at a position the list doesn't have yet. Add new items with append().

Broken
nums = []
nums[0] = 5
print(nums)
IndexError: list assignment index out of range
Fixed
nums = []
nums.append(5)
print(nums)
[5]
tuple index out of range

A tuple counts from 0 like a list: a pair has positions 0 and 1.

Broken
point = (3, 4)
print(point[2])
IndexError: tuple index out of range
Fixed
point = (3, 4)
print(point[1])
4
Replacement index 1 out of range for positional args tuple

Each {} in the text needs a value in .format(). An f-string puts each value in its place and can't miss one: f"{name} is {age}".

Broken
name = "Ada"
print("{} is {} years old".format(name))
IndexError: Replacement index 1 out of range for positional args tuple
Fixed
name = "Ada"
print("{} is {} years old".format(name, 36))
Ada is 36 years old
pop index out of range

pop(i) takes the item at position i, so the list needs one there. The first is pop(0), and pop() with no position takes the last.

Broken
queue = ["Ada", "Bo"]
print(queue.pop(2))
IndexError: pop index out of range
Fixed
queue = ["Ada", "Bo"]
print(queue.pop(0))
Ada
Cannot choose from an empty sequence

random.choice() needs a list with something in it. Check first: if names: is true only when the list has items.

index 3 is out of bounds for axis 0 with size 3

numpy's way of saying list index out of range: an array of size 3 has positions 0, 1 and 2. a.shape shows the size of each axis.

Still stuck?