In Python, lists, tuples are indexed. It means each value in a tuple is associated with index position (0 to n-1) to access that value. Where N represent the total number of values in list or tuple. When user try access an item in a tuple that is out of range the Python returns an error that says “IndexError: tuple index out of range”.
Example: IndexError: tuple index out of range
Lets consider a below example for tuple of fruits. Where index of value start from 0 and up to (number of element -1).
fruits = ("Apple", "Banana", "Grapes", "Papaya", "Litchi")
This tuple fruits is having five values and each element is associated with index number as below:
Apple | Banana | Grapes | Papaya | Litchi |
0 | 1 | 2 | 3 | 4 |
To access the value “Grapes” from fruits tuple, we would use this code:
print(fruits[2])
Our code returns: Grapes. here we accessing the value at the index position 2 and print it to the console. Same way we can try with other values in tuple.
Now lets consider an example to create this IndexError, Try to access value by using index value out of range (3 to 6) where index position 5 is out of range and this example will throw exception as “IndexError: tuple index out of range“.
fruits = ("Apple", "Banana", "Grapes", "Papaya", "Litchi")
for i in range(2, 6):
print(fruits[i])
Output
Grapes
papaya
Lichi
Traceback (most recent call last):
File "main.py", line 4, in <module>
print(fruits[i])
IndexError: tuple index out of range
Our code prints out the values Grapes, Papaya and Litchi. These are the last three values in our tuple. Then throw exception as “IndexError: tuple index out of range” because index position 5 is out of the range for elements in the tuple.
The Solution
Our range() statement creates a list of numbers between the range of 2 and 6. This number list is inclusive of 2 and exclusive of 6. Our fruits tuble is only indexed up to 4. This means that our loop range will try to access a fruit at the index position 5 in our tuple because 5 is in our range.
Now lets try to run this below updated program for loop range 2 to 5 then observe the result. To learn more on for loop follow link Python: for loop
fruits = ("Apple", "Banana", "Grapes", "Papaya", "Litchi")
for i in range(2, 5):
print(fruits[i])
Output
Grapes
papaya
Litchi
Our code successfully prints out the last three items in our list because now accessing items at the index positions 2, 3, and 4 which is in range of fruit tuple indexes.
Conclusion
The IndexError: tuple index out of range error occurs when you try to access an item in a tuple that does not exist. To solve this problem, make sure that whenever you access an item from a tuple that the item for which you are looking exists.
To learn more on exception handling follow the link Python: Exception Handling.
If this blog for solving IndexError help you to resolve problem make comment or if you know other way to handle this problem write in comment so that it will help others.
One thought on “[Solved]: Python IndexError: tuple index out of range”