HOWTO · Python

How to Fix IndexError: Tuple Index Out of Range in Python

We will demonstrate how to resolve tuple index out of range error in Python.

On this page

A tuple index error means Python was asked to retrieve an item at a position the tuple does not contain. This guide explains how indexes work, how to select the final item, and how to handle an index that depends on changing data.

What IndexError: tuple index out of range means

A tuple is an ordered, immutable collection. Python indexes its items starting at 0, so a tuple with n items has valid positive indexes from 0 through n - 1. For example, the final item of a seven-item tuple is at index 6, not 7. Trying to read index 7 raises IndexError: tuple index out of range.

Negative indexes count backward from the end: -1 selects the final item and -2 the item before it. An empty tuple has no items, so neither positive nor negative indexing can select an item from it.

Common causes of the error

The most frequent cause is an off-by-one calculation, often using the tuple’s length as an index. If items has four elements, len(items) is 4, but its final positive index is 3. A loop that uses range(len(items)) is safe for ordinary indexing because its final value is one less than the length; manually adding one can go too far.

The index may also come from input, a calculation, or data that changes between steps. Check the value actually used at the failing access, along with the tuple’s current length. A different value type does not itself explain this tuple-specific message; confirm what object is being indexed if the traceback or variable name is misleading.

Access the last tuple item safely

For a tuple that contains at least one item, items[-1] is the concise way to get the last item. If the length is already needed for other logic, items[len(items) - 1] selects the same position. Both forms require at least one item.

Run this example:

items = ("red", "green", "blue")
print(items[-1])
blue

If the tuple may contain no items, check it before indexing:

items = ()
if items:
    print(items[-1])
else:
    print("No items")
No items

This handles the no-item case explicitly instead of attempting an access that cannot succeed.

Handle indexes that may be out of range

When an index is expected to be within the tuple, validate it before access. This also makes it easy to choose a default or report that the requested position is unavailable:

items = ("red", "green", "blue")
index = 4
if 0 <= index < len(items):
    print(items[index])
else:
    print("Index is outside the tuple")
Index is outside the tuple

A bounds check is useful when the program needs a particular response to an unavailable position. Catch IndexError when the access is part of an operation where the index can legitimately vary and handling the failure is clearer than checking first. Keep the try block focused on that access; broad exception handling can hide unrelated programming errors. An empty tuple has no index that passes the positive-index check.

Check the value and index before indexing

If the object’s type is uncertain, first confirm it is a tuple with isinstance(value, tuple). This check addresses the object’s type; it does not guarantee a particular index is present. Then check the index against len(value) or handle IndexError at the access. If the object is another sequence type, investigate its own expected behavior rather than assuming it caused the tuple-specific message.

In short, use IndexError to identify an attempted access beyond a tuple’s available positions. Correct off-by-one calculations, use items[-1] for the final item of a nonempty tuple, and choose an explicit bounds check or focused exception handling when indexes depend on data.