IndexError: tuple index out of range (Fix)

IndexError: tuple index out of range means your code is trying to read a tuple position that does not exist.

This usually happens when you use an index that is too large, or when you forget that Python indexes start at 0.

Quick fix #

colors = ("red", "green", "blue")

# Wrong: colors[3]  # IndexError

# Correct: use a valid index
print(colors[2])

# Or check length first
index = 3
if index < len(colors):
    print(colors[index])
else:
    print("Index out of range")

Tuple indexes start at 0. A tuple with 3 items has valid indexes 0, 1, and 2.

What this error means #

Python raises this error when you try to access a tuple index that is outside the tuple’s valid range.

Important points:

  • Tuple indexing starts at 0, not 1
  • A tuple with 3 items has indexes 0, 1, and 2
  • Negative indexes work from the end, but they must still be valid

If you are new to tuples, see Python tuples explained.

Example that causes the error #

Here is a small tuple with 3 items:

colors = ("red", "green", "blue")

print(colors[3])

Output:

IndexError: tuple index out of range
red0-3green1-2blue2-1
Valid indices are 0, 1, 2 (or -1, -2, -3). colors[3] has no fourth item.
Traceback (most recent call last):File "example.py", line 3, in <module>print(colors[3])IndexError: tuple index out of rangeWhere it happened — file and lineWhat went wrong — the exception typeWhy — the detailed message
Index 3 is past the end of a 3-item tuple.

Why this fails:

  • The tuple has 3 items
  • The valid indexes are 0, 1, and 2
  • colors[3] asks for a fourth item, but there is no fourth item

The last valid index is 2.

If you want a clearer explanation of positions in tuples, see Python tuple indexing explained.

Why it happens #

This error usually happens for one of these reasons:

  • Using an index that is too large
  • Using a negative index that goes past the start of the tuple
  • Assuming the tuple has more items than it really has
  • Using loop logic that goes one step too far

For example, this fails because the negative index is too small for the tuple:

numbers = (10, 20, 30)

print(numbers[-4])

A 3-item tuple supports these negative indexes:

  • -1 for the last item
  • -2 for the second-to-last item
  • -3 for the first item

But -4 is outside the valid range.

How to fix it #

Use the correct index #

If you know the tuple size, use a valid index.

colors = ("red", "green", "blue")

print(colors[0])  # red
print(colors[2])  # blue

Check the tuple length with len() #

If the index may change, check it before accessing the tuple.

colors = ("red", "green", "blue")
index = 3

if 0 <= index < len(colors):
    print(colors[index])
else:
    print("Index out of range")

This is safer when the tuple size is not guaranteed.

If needed, read more about Python tuple length with len().

Loop over the tuple directly #

If you want every item, you often do not need indexes at all.

colors = ("red", "green", "blue")

for color in colors:
    print(color)

This avoids index mistakes completely.

Use enumerate() when you need both index and value #

If you need the position and the item, enumerate() is safer than manual indexing.

colors = ("red", "green", "blue")

for i, color in enumerate(colors):
    print(i, color)

Output:

0 red
1 green
2 blue

See also Python enumerate() function explained.

Debugging steps #

When you see this error, check these things first:

  1. Print the tuple to see what it really contains
  2. Print its length
  3. Print the index value before the failing line
  4. Confirm that your code is using 0-based indexing

Useful debugging commands:

print(my_tuple)
print(len(my_tuple))
print(index)

for i, value in enumerate(my_tuple):
    print(i, value)

Example:

user = ("Alice", 25)
index = 2

print(user)
print(len(user))
print(index)
print(user[index])

Output before the error:

('Alice', 25)
2
2

This tells you:

  • The tuple has only 2 items
  • Valid indexes are 0 and 1
  • Index 2 is not valid

Special case: negative indexes #

Negative indexes count from the end of the tuple:

  • my_tuple[-1] means the last item
  • my_tuple[-2] means the second-to-last item

Example:

letters = ("a", "b", "c")

print(letters[-1])  # c
print(letters[-2])  # b

But negative indexes can still fail if the tuple is shorter than expected:

letters = ("a", "b", "c")

print(letters[-4])

This raises:

IndexError: tuple index out of range

How to avoid this error in the future #

  • Do not guess tuple positions
  • Check the structure of returned data before indexing
  • Prefer unpacking when you know the tuple size
  • Use loops for all items instead of hard-coded indexes

For example, unpacking is often clearer than manual indexing:

point = (10, 20)

x, y = point

print(x)
print(y)

This is easier to read than:

point = (10, 20)

print(point[0])
print(point[1])

Common mistakes #

These are very common causes of this error:

  • Accessing index 3 in a tuple that only has 3 items
  • Using 1-based thinking instead of Python’s 0-based indexing
  • Looping with range(len(my_tuple) + 1)
  • Using a negative index smaller than the tuple allows
  • Reading function return values and assuming the tuple shape incorrectly

Here is a common loop mistake:

values = (10, 20, 30)

for i in range(len(values) + 1):
    print(values[i])

Why it fails:

  • len(values) is 3
  • range(len(values) + 1) gives 0, 1, 2, 3
  • Index 3 does not exist

Correct version:

values = (10, 20, 30)

for i in range(len(values)):
    print(values[i])

Or even better:

values = (10, 20, 30)

for value in values:
    print(value)

If you are seeing the same problem with a list, read IndexError: list index out of range.

FAQ #

Are tuples 0-indexed in Python? #

Yes. The first item is at index 0.

Can negative indexes cause this error? #

Yes. If the negative index goes beyond the start of the tuple, Python raises the same error.

How is this different from list index out of range? #

The cause is basically the same, but this error happens with tuples instead of lists.

Should I use len() before every tuple access? #

Not always. Use it when the tuple size may change or when the index is not guaranteed to be valid.

See also #

Press Esc to close