IndexError: tuple index out of range in Python

The index for a tuple works the same as the index for a list in Python. In Python, we can access items/elements of iterable like lists and tuples using an index. However, an error named “IndexError: tuple index out of range” occurs in Python when the accessed item/element does not exist in the tuple.

In this blog post, various examples explain the reasons and solutions for the stated error:

So, let’s begin our tutorial!

How to Resolve “IndexError: tuple index out of range” in Python?

To resolve the “tuple index out of range” error, various solutions are used in Python. Every element value stored in a tuple contains an index number that will be used to access that element in the program. The first element in Python tuple has a “0” index and will be incremented by “1” when moving to the next element. To access the tuple from the end, the “-1” index value will be used in the program.

Reason: Access an Index That Does not Exist

The prominent reason that causes this error in Python script is to access the index of the tuple that does not exist in the given range. The below block diagram shows the “IndexError” of the Python program:

The above output shows the error because the tuple index is out of range.

Solution 1: Access Index Within Range

To rectify the stated error, the tuple index must be accessed within the given range. For example, the “len()” function is used along with the if-else statement to get the range or length of the tuple.

Code:

tuple_value = (2,5,6,9)
len_index = len(tuple_value)

indx = int(input('Enter the Index Value: '))

if len_index > indx:
    print(tuple_value[indx])
else:
    print('Index is Out of Range')

In the above code:

  • The tuple is initialized and the “len()” function is used to get the range of tuples.
  • After knowing/getting the range, the “input()” function is used to get the value from the user and convert it into an integer using the inbuilt “int()” function.
  • The “if” statement executes the code inside the if block when the length of the index is greater than the input index.
  • When the index length is smaller than the input index, the else block shows a message on the screen that the index value is out of range.

Output:

The above output shows the index value of the tuple when the user enters the index within the given range. It shows an error when the user enters the out-of-range index.

Solution 2: Iterate Over the Tuple

To fix this error, the “range()” and “enumerate()” function is used to iterate over the tuple and print all the index values within the given range.

Code:

tuple_value = (2,5,6,9)

print('Using Simple for loop')
for i in tuple_value:
    print(i)

print('Using range() Function: ')
for i in range(len(tuple_value)):
    print(i, tuple_value[i])

print('\nUsing enumerate() Function: ')
for index, item in enumerate(tuple_value):
    print(index, item)

In the above code:

  • The tuple is created and stored in a variable named “tuple_value”.
  • The above code utilizes three simple techniques to print the index value of the tuple within a given range.
  • In the first part, the simple “for” loop iterates over the tuple and prints all the index values on the console screen.
  • In the second part, the “for” loop iterates over the tuple using the “range()” function and with the help of the length of the tuple. This method returns indexes along with tuple values at that particular index.
  • Lastly, the “for” loop also iterates over the tuple using enumerate() function with the index in a much better way.

Output:

This way, you can resolve the “tuple index out of range” error in Python.

That’s it from this guide!

Conclusion

The error “IndexError: tuple index out of range” occurs in Python when the accessed item does not exist in the given range of the tuple. To rectify this error, users ensure that the item to be accessed must exist in the given range of tuples. The inbuilt “len()” function is used to find the range of the tuple, and using the “if-else” condition, we can handle the index error in the Python program. The traditional “for” loop, range() function, and “enumerate()” functions are used in Python to rectify the index error. This post provided several solutions to rectify the “IndexError: tuple index out of range” error in Python.

500M+ Views on Our Blogs & YouTube Channels till Now. We are helping 10 Businesses join our Agency and become a part of our growth. Click here if you are interested. ×