Like lists, Python tuples are indexed. This means each value in a tuple has a number you can use to access that value. When you try to access an item in a tuple that does not exist, Python returns an error that says “tuple index out of range”.
In this guide, we explain what this common Python error means and why it is raised. We walk through an example scenario with this problem present so that we can figure out how to solve it.
Problem Analysis: IndexError: tuple index out of range
Tuples are indexed starting from 0. Every subsequent value in a tuple is assigned a number that is one greater than the last. Take a look at a tuple:
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 |
0 | 1 | 2 | 3 | 4 |
To access the value “Robin” in our tuple, we would use this code:
print(birds[0])
Our code returns: Robin. We access the value at the index position 1 and print it to the console. We could do this with any value in our list.
If we try to access an item that is outside our tuple, an error will be raised.
An Example Scenario
Let’s write a program that prints out the last three values in a tuple. Our tuple contains a list of United Kingdom cities. Let’s start by declaring a tuple:
cities = ("Edinburgh", "London", "Southend", "Bristol", "Cambridge")
Next, we print out the last three values. We will do this using a for loop and 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.
Here is the code for our for loop:
for i in range(3, 6): print(birds[i])
Let’s try to run our code:
Goldfinch Chaffinch Traceback (most recent call last): File "main.py", line 4, in <module> print(birds[i]) IndexError: tuple index out of range
Our code prints out the values Goldfinch and Chaffinch. These are the last two values in our list. It does not print out a third value.
The Solution
Our range()
statement creates a list of numbers between the range of 3 and 6. This list is inclusive of 3 and exclusive of 6. Our 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.
Let’s see what happens if we try to print out birds[5] individually:
print(birds[5])
Our code returns:
Traceback (most recent call last): File "main.py", line 3, in <module> print(birds[5]) IndexError: tuple index out of range
The same error is present. This is because we try to access items in our list as if they are indexed from 1. Tuples are indexed from 0.
To solve this error, we need to revise our range()
statement so it only prints the last three items in our tuple. Our range should go from 2 to 5:
for i in range(2, 5): print(birds[i])
Let’s run our revised code and see what happens:
Great Tit Goldfinch Chaffinch
Our 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
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.
The most common cause of this error is forgetting that tuples are indexed from 0. Start counting from 0 when you are trying to access a value from a tuple. As a beginner, this can feel odd. As you spend more time coding in Python, counting from 0 will become second-nature.
Now you’re ready to solve this Python error like an expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.