How to Fix the IndexError tuple index out of range Error in Python?

In this tutorial, I will explain how to fix the IndexError tuple index out of range error in Python. As a developer working on various projects, I encountered an error called IndexError, which made me research more on this topic. I will share my findings with suitable examples and screenshots.

IndexError tuple index out of range Error in Python

The “IndexError: tuple index out of range” error occurs when you try to access an item in a tuple using an invalid index. This means you are attempting to retrieve an element from a tuple at an index position that does not exist.

For example, let’s say we have a tuple called student_grades containing the grades for a student named John from New York:

student_grades = (85, 92, 78, 90)

This tuple has 4 elements, at index positions 0 through 3. If we try to access an element at index 4 or higher, we will get the “tuple index out of range” error:

print(student_grades[4]) 

Output:

IndexError: tuple index out of range

You can see the output in the screenshot below.

IndexError tuple index out of range Error in Python

Simply put, this error means you are trying to access an index of a tuple object that is beyond its length. It’s similar to attempting to access the 6th item of a list that only contains 5 elements.

Read How to Create a Tuple from the List in Python?

Common Causes of “IndexError: tuple index out of range”

There are a few common scenarios that can lead to this error:

  1. Forgetting that tuple indexes start at 0: Remember that in Python, the first element of a tuple is at index 0, not 1. So in a tuple with 4 items, the valid indexes are 0, 1, 2, and 3.
  2. Using the wrong variable: Double-check that you are accessing the index on the correct tuple variable. It’s an easy mistake to make if you have several similarly named variables.
  3. Miscounting the number of elements: Be sure to count the number of elements in your tuple carefully. It’s easy to think there are more elements than there are.

How to Fix “IndexError: tuple index out of range”

The solution depends on what you are trying to accomplish by accessing an out-of-range index. Here are a few possibilities:

Check out How to Find the Length of a Tuple in Python?

Check if an index exists before accessing it

If you are not sure whether an index exists, you can first check the length of the tuple using the len() function. For example:

student_grades = (85, 92, 78, 90) 

index = 4
if index < len(student_grades):
    print(student_grades[index])
else:
    print(f"Index {index} is out of range for the student_grades tuple")

Output:

Index 4 is out of range for the student_grades tuple

You can see the output in the screenshot below.

Fix the IndexError tuple index out of range Error in Python

Read Python Set vs Tuple

Catch the IndexError exception

Another option is to wrap the tuple access in a try/except block to catch the IndexError if it occurs:

student_grades = (85, 92, 78, 90)

try:
    print(student_grades[4])
except IndexError:
    print("Oops, that index is out of range!")

Output:

Oops, that index is out of range!

You can see the output in the screenshot below.

How to Fix the IndexError tuple index out of range Error in Python

Check out How to Access Tuple Elements in Python?

Use a default value

If you are okay with providing a default value when an index doesn’t exist, you can use the get() method of a tuple (Python 3.7+):

student_grades = (85, 92, 78, 90)

print(student_grades.get(4, "Index not found")) 

This will print “Index not found” for any index that is out of range for the tuple.

Read How to Print a Tuple in Python?

Iterate over the tuple instead of using indexes

In many cases, you don’t need to use indexes at all. You can just iterate over the elements of the tuple directly in a for loop:

student_grades = (85, 92, 78, 90)

for grade in student_grades:
    print(grade)

This will print out each grade on a separate line without the risk of an IndexError.

Debug Tips

If you are still getting the “IndexError: tuple index out of range” error after trying the above solutions, here are a few more debugging tips:

  1. Print out the tuple: Use print(your_tuple) to visually confirm the contents and length of the tuple.
  2. Print out the index variable: If you are using a variable to store the index, print out its value to make sure it contains what you expect.
  3. Use an IDE with debugging tools: IDEs like PyCharm or Visual Studio Code have built-in debugging features that can help you step through your code line by line to identify issues.

Check out How to Check if a Tuple is Empty in Python?

Conclusion

In this article, I helped you to understand how to IndexError tuple index out of range error in Python. I discussed some common causes of IndexError. I also discussed how to fix the error by checking if an index exists before accessing it, catching the IndexError exception, using a default value, and iterating over the tuple instead of using indexes. Additionally, I gave a few debug tips.

You can also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.