What does tuple index out of range mean?
You don’t have a «fifth» (or rather, index 5) element because the first element has index 0.
Therefore you have to have 6 elements to use index 5.
Here, your elements are:
-
The Overflow Blog
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.8.2.43556
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Tuple Index Out of Range Error in Python
Tuples in Python are immutable data structures used to store data in a sequential manner. In this article, we will discuss the tuple index() method in Python. We will also discuss how to get the index of an element in a tuple in Python.
The Tuple index() Method in Python
The index() method is used to find the index of an element in a tuple in Python. When invoked on a tuple, the index() method takes a value as its input argument. After execution, it returns the index of the leftmost occurrence of the element in the tuple. You can observe this in the following example.

In this example, there are three instances of element 44 in the tuple at indices 2,5, and 7. However, the index() method returns the value 2 as it considers only the leftmost index of an element.
If the value passed to the index() method isn’t present in the tuple, the program runs into a ValueError exception with the message “ ValueError: tuple.index(x): x not in tuple ”. You can observe this in the following example.
In this example, the program ran into the ValueError exception as the element 1117 isn’t present in the tuple.
Find All The Indices of A Given Element in a Tuple in Python
Instead of using the index() method, we can also use for loop to find the index of an element in a tuple in Python. For this, we will use the following steps.
- First, we will calculate the length of the tuple using the len() function. The len() function takes the tuple as its input argument and returns the length of the tuple. We will store the value in the variable “ tupleLength ”.
- Next, we will use a for loop and the range() function to iterate over the elements of the tuple. While iteration, we will first check if the current element is equal to the element we are searching for. If yes, we will print the current index.
After executing the for loop, we will get all the indices of the given element in the tuple as shown below.
Solved: Python Tuple Index Out of Range Error
We can access an element of a tuple in Python using the indexing operator as shown below.
In the above example, the index passed to the indexing operator should be less than the length of the tuple. If the value passed to the indexing operator is greater than or equal to the length of the tuple, the program will run into an IndexError exception with the message “I ndexError: tuple index out of range ”. You can observe this in the following example.
In this example, we have passed index 23 to the indexing operator. As 23 is greater than the length of the tuple, the program runs into the Python IndexError exception.
To solve this problem, you can first check if the value passed to the indexing operator is greater than or equal to the length of the tuple. If yes, you can inform the user that the index should be less than the length of the tuple as shown below.
In this example, we first check if the index value is greater than the length of the tuple. If yes, we print the message that index is greater than the length of the tuple. Otherwise, the program prints the value at the given index. Here, we are preempting the error.
Instead of the above approach, we can use the Python try except block to handle the IndexError exception once it is raised by the program as shown below.
In this example, the IndexError exception is raised in the try block of the code. Then, the except block catches the exception and prints the message. Here, we are handling the error after it has already occurred.
Conclusion
In this article, we discussed how to find the index of an element in a tuple in Python. We also discussed how to solve the tuple index out-of-range error using the if-else statement and the try-except blocks. To learn more about Python programming, you can read this article on how to sort a tuple in Python. You might also like this article on the with open statement in python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Related
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.
How To Solve “IndexError: tuple index out of range” in Python
In this article, we will explain what causes IndexError: tuple index out of range in Python. Some common causes of this error such as incorrect comparison conditions in loops or wrong initialization values. Let’s go into the specific situation and figure out how to fix it.
Table of Contents
What causes the error “IndexError: tuple index out of range” in Python
The error occurs when you want to go to an index that is out of range of the tuple. Look at this program that prints out the values in a tuple.
Output:
And when we change range(3) to range(4), the above error occurs:
Output:
The tuple has only 3 indexes: 0,1,2 but for loop wants to access the 3rd index position, so the error happens.
Solutions for “IndexError: tuple index out of range” error
Using For loop
We can use len() function to check the length of the tuple first and then use a for loop to iterate over the items so we avoid the error.
Example:
Output:
Using While Loop
Output:
len() function will return a tuple length of 3, so the loop will run 4 times because the initial value of i is 0. We will not use “<=” in this case, but should only use “<“, the variable i will not be able to equal the length of the tuple, and the code will not access the 4th element of the tuple. So the error won’t happen.
Using Try… Except to catch this error
We should only loop within the tuple’s index range. Avoid letting the loop access out of the tuple’s scope and make sure the item exists in the tuple. You can use Try… Except to catch this error, preventing the program from stopping.
Output:
Summary
The error “IndexError: tuple index out of range” in Python has been solved. In many cases, It would help if you used the len() function to check the range of the tuple first. Pay attention to the index range of the tuple while writing the program so you can avoid this error. We hope you like it. Good luck to you!

Carolyn Hise has three years of software development expertise. Strong familiarity with the following languages is required: Python, Typescript/Nodejs, .Net, Java, C++, and a strong foundation in Object-oriented programming (OOP).
How To Fix IndexError: Tuple Index Out Of Range Error

The IndexError: Tuple Index Out Of Range error is a common type of index error that arises while dealing with tuple-related codes. These errors are generally related to indexing in tuples which are pretty easy to fix.
This guide will help you understand the error and how to solve it.
What is a Tuple?
Tuples are one of the four built-in single-variable objects used to store multiple items. Similar to other storage for data collection like List, Sets, and, Dictionary, Tuple is also a storage of data that is ordered and unchangeable. Similar to lists, the items here are also indexed. It is written with round brackets. Unlike other storage data sets, Tuples can have duplicate items, which means the data in a tuple can be present more than once. Also, Tuples can have different data types, such as integers, strings, booleans, etc.

To determine the length of a tuple, the len() function is used. And to get the last item in a tuple, you need to use [- index number of the item you want].
Please enable JavaScript
What is IndexError?
An IndexError is a pretty simple error that arises when you try to access an invalid index. It usually occurs when the index object is not present, which can be caused because the index being more extensive than it should be or because of some missing information. This usually occurs with indexable objects like strings, tuples, lists, etc. Usually, the indexing starts with 0 instead of 1, so sometimes IndexError happens when one individual tries to go by the 1, 2, 3 numbering other than the 0, 1, 2 indexing.
For example, there is a list with three items. If you think as a usual numbering, it will be numbered 1, 2, and 3. But in indexing, it will start from 0.
What is IndexError: Tuple Index Out Of Range?
The IndexError: tuple index out-of-range error appears when you try to access an index which is not present in a tuple. Generally, each tuple is associated with an index position from zero “0” to n-1. But when the index asked for is not present in a tuple, it shows the error.
Causes for IndexError: Tuple Index Out Of Range?
In general, the IndexError: Tuple Index Out Of Range error occurs at times when someone tries to access an index which is not currently present in a tuple. That is accessing an index that is not present in the tuple, which can be caused due to the numbering. This can be easily fixed if we correctly put the information.
This can be understood by the example below. Here the numbers mentioned in the range are not associated with any items. Thus this will raise the IndexError: Tuple Index Out Of Range.
Solution for IndexError: Tuple Index Out Of Range
The IndexError: tuple index out-of-range error can be solved by following the solution mentioned below.
For example, we consider a tuple with a range of three, meaning it has three items. In a tuple, the items are indexed from 0 to n-1. That means the items would be indexed as 0, 1, and 2.
In this case, if you try to access the last item in the tuple, we need to go for the index number 2, but if we search for the indexed numbered three, it will show the IndexError: Tuple Index Out Of Range error. Because, unlike a list, a tuple starts with 0 instead of 1.
So to avoid the IndexError: Tuple Index Out Of Range error, you either need to put the proper range or add extra items to the tuple, which will show the results for the searched index.