Skip to content

Latest commit

 

History

History
63 lines (39 loc) · 1.29 KB

File metadata and controls

63 lines (39 loc) · 1.29 KB

IndexError: tuple index out of range

IndexError: tuple index out of range

This error occurs when you try to access a tuple position that does not exist.
Tuples, like lists, use zero-based indexing.


Example (Reproducing the Error)

values = (10, 20, 30)
print(values[3])

The tuple contains three elements.
Valid indexes are 0, 1, and 2.
Index 3 is out of range.


Why This Happens

Tuples follow the same indexing rules as lists:

  • Indexing starts at 0
  • The maximum valid index is length - 1

Even though tuples are immutable, their indexing behavior is identical to lists.

If you're unsure about tuple behavior compared to lists, review the tuples and ranges tutorial on pyai.io.


Defensive Pattern

Always check boundaries before accessing a tuple:

values = (10, 20, 30)
index = 3

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

Related List and Tuple Index Errors


If you'd like a broader explanation of list index errors, see the main guide: