- To solve the “IndexError: tuple index out of range”, avoid do not access a non-existing tuple index. For example,
my_tuplecauses an error for a tuple with three elements.
- If you access tuple elements in a loop, keep in mind that Python uses zero-based indexing: For a tuple with
nelements, the first element has index
0and the last index
- A common cause of the error is trying to access indices
1, 2, ..., ninstead of using the correct indices
0,1, ..., (n-1).
The following video shows how I fixed a similar error on a list instead of a tuple:
If you’re like me, you try things first in your code and fix the bugs as they come.
One frequent bug in Python is the
IndexError: tuple index out of range. So, what does this error message mean?
The error “tuple index out of range” arises if you access invalid indices in your Python tuple. For example, if you try to access the tuple element with index 100 but your tuple consist only of three elements, Python will throw an IndexError telling you that the tuple index is out of range.
Here’s a screenshot of this happening on my Windows machine:
Let’s have a look at an example where this error arises:
my_tuple = ('Alice', 'Bob', 'Carl') print(my_tuple)
The element with index 3 doesn’t exist in the tuple with three elements. Why is that?
The following graphic shows that the maximal index in your tuple is 2. The call
my_tuple would retrieve the third tuple element
my_tuple --> Alice
my_tuple --> Bob
my_tuple --> Carl
my_tuple --> ??? Error ???
Did you try to access the third element with index 3?
It’s a common mistake: The index of the third element is
2 because the index of the first tuple element is
How to Fix the IndexError in a For Loop? [General Strategy]
So, how can you fix the code? Python tells you in which line and on which tuple the error occurs.
To pin down the exact problem, check the value of the index just before the error occurs.
To achieve this, you can print the index that causes the error before you use it on the tuple. This way, you’ll have your wrong index in the shell right before the error message.
Here’s an example of wrong code that will cause the error to appear:
# WRONG CODE my_tuple = ('Alice', 'Bob', 'Ann', 'Carl') for i in range(len(my_tuple)+1): my_tuple[i] ''' OUTPUT Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 5, in <module> my_tuple[i] IndexError: tuple index out of range '''
The error message tells you that the error appears in line 5.
So, let’s insert a
my_tuple = ('Alice', 'Bob', 'Ann', 'Carl') for i in range(len(my_tuple)+1): print(i) my_tuple[i]
The result of this code snippet is still an error.
But there’s more:
0 1 2 3 4 Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 6, in <module> my_tuple[i] IndexError: tuple index out of range
You can now see all indices used to retrieve an element.
The final one is the index
i=4 which points to the fifth element in the tuple (remember zero-based indexing: Python starts indexing at index 0!).
But the tuple has only four elements, so you need to reduce the number of indices you’re iterating over.
The correct code is, therefore:
# CORRECT CODE my_tuple = ('Alice', 'Bob', 'Ann', 'Carl') for i in range(len(my_tuple)): my_tuple[i]
Note that this is a minimal example and it doesn’t make a lot of sense. But the general debugging strategy remains even for advanced code projects:
- Figure out the faulty index just before the error is thrown.
- Eliminate the source of the faulty index.
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.