When you try to access an item in a tuple that does not exist, Python returns an error that says “Python tuple index out of range”.
So, in this guide, we explain what this common Python error means and why it is raised. We also walk through an example scenario with this problem present so that we can figure out how to solve it.
IndexError: Python tuple index out of range – Discussion of the problem
To clarify, tuples are indexed starting from 0. Every subsequent value in a tuple is assigned a number that is one greater than the last. so, take a look at a tuple:
1 birds = ("Robin", "Collared Dove", "Great Tit", "Goldfinch", "Chaffinch")
This tuple contains five values. Each value has its own index number:
|Robin||Collared Dove||Great Tit||Goldfinch||Chaffinch|
An Example Scenario
Let’s write a program that prints out the last three values in a tuple. To clarify, the tuple contains a list of United Kingdom cities. So , let’s start by declaring a tuple:
1 cities = ("Edinburgh", "London", "Southend", "Bristol", "Cambridge")
Next, we print out the last three values. We will do this using a for loop as well as a
range() statement. The range() statement creates a list of numbers between a particular range so that we can iterate over the items in our list whose index numbers are in that range.
So, here is the code for our for loop:
1 for i in range(3, 6): 2 print(birds[i])
Let’s try to run our code:
1 Goldfinch 2 Chaffinch 3 Traceback (most recent call last): 4 File "main.py", line 4, in <module> 5 print(birds[i]) 6 IndexError: tuple index out of range
The code prints out the values Goldfinch as well as Chaffinch. These are the last two values in our list. It also does not print out a third value.
The Solution of Python tuple index out of range
range() statement creates a list of numbers between the range of 3 and 6. This list is also inclusive of 3 and exclusive of 6. The list is only indexed up to 4. This means that our loop will try to access a bird at the index position 5 in our tuple because 5 is in our range.
Most important, let’s see what happens if we try to print out birds individually:
Then, the code returns:
1 Traceback (most recent call last): 2 File "main.py", line 3, in <module> 3 print(birds) 4 IndexError: tuple index out of range
Here also presents the same error in the same vein. This is because we try to access items in our list as if they are indexed from 1. Tuples are indexed from 0.
So, to solve this error: Python tuple index out of range, we need to revise our
range() statement. Therefore, it only prints the last three items in our tuple. The range should go from 2 to 5:
1 for i in range(2, 5): 2 print(birds[i])
Let’s run our revised code and see what happens:
1 Great Tit 2 Goldfinch 3 Chaffinch
The code successfully prints out the last three items in our list. We’re now accessing items at the index positions 2, 3, and 4. All of these positions are valid so our code now works.
Conclusion on Python tuple index out of range
The IndexError: Python 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.
Need to find a reliable Web/Mobile app development partner? Let’s ArrowHiTech help you!
Above all, with high skill and professional services, we can help you open your perfect in the future. If you still have problems, you can contact here now and we will provide you with the best results.
Our offered custom development services