. Advertisement .
..3..
. Advertisement .
..4..
Python is a language that has a variety of projects to try to learn. When you are learning and trying to execute commands, you may encounter some errors, and IndexError: tuple index out of range is one of these.
The IndexError occurs when you call an index value that is out of range. So, for any error that starts with IndexError, you need to know it’s about the index value.
Let’s checkout how to fix IndexError: tuple index out of range
How to fix IndexError: tuple index out of range?
This IndexError occurs when you are trying to access an item that doesn’t exist in a tuple. Let’s take a look at the methods and examples to solve IndexError: tuple index out of range
Use Indexing Method
For indexing, the basic concept is that each value in the tuple is linked with the index position to access the value. Index position is from 0 to n-1, where N is the total number of tuple’s values.
Example
1 | cars= (‘Bentley’, ‘Mercedes’, ‘Audi’, ‘BMW’, ‘Lexus’) |
Having index numbers:
Bentley | Mercedes | Audi | BMW | Lexus |
0 | 1 | 2 | 3 | 4 |
To obtain the value of ‘Mercedes’ from the tuple, write
print(cars[1])
Use Length Method
The Len()
is used to obtain the length of a tuple. Take a look at the example
# Declare tuple
cars= ('Bentley', 'Mercedes', 'Audi', 'BMW', 'Lexus')
print('Print length of Tuple: ',len(cars))
i=0
# While loop less than and equal to tuple "cars" length.
while i <= len(cars):
print(cars[i])
i += 1
Error message
File "pyprogram.py", line 8
print(cars[i])
SyntaxError: invalid character in identifier
The function counts the tuple length as ‘5’, whereas the loop runs 6 times from value i=0 to i=5, which is why we get out of range.
Solutions
# Declare tuple
cars= ('Bentley', 'Mercedes', 'Audi', 'BMW', 'Lexus')
i=0
print('Print length of Tuple: ',len(cars))
# While loop less than tuple "cars" length.
while i < len(cars):
print(cars[i])
i += 1
Output
Print length of Tuple: 5 Bentley Mercedes Audi BMW Lexus
Use range() Method
In the range()
method, we provide a specific range of values in the tuple. Its function is to create a list of numbers between the provided range to iterate the items present in our list, which has index numbers in that range.
Example
countries = ('Portugal', 'Hungary', 'Canada', 'Jamaica', 'Brazil', 'Israel')
for i in range(3, 7):
print(countries[i])
Error message
Traceback (most recent call last):
File "main.py", line 4, in <module>
print(countries[i])
IndexError: tuple index out of range
It can’t print the value because out of the range.
Solution
We need to revise the range()
. It should be from 2 to 6
for i in range(2, 6):
print(countries[i])
Output
Canada
Jamaica
Brazil
Israel
Use the try/except method
In this method two possibilities are provided to the program; try or except/catch
. Let’s look at the example
cities = ('Venice', 'Frankfurt', 'London')
index_to_check = 3
try:
thing = cities[index_to_check]
except IndexError:
thing = "empty"
print(f"The value of thing is '{thing}'")
Output
The value of thing is 'empty'
Use enumerate() method
To iterate over a tuple, the best solution is to use enumerate()
function. It is known to return enumerate object with a tuple by taking an iterable. Let’s check the example
cities = ('Venice', 'Frankfurt', 'London')
for index, item in enumerate(cities):
print(index, item)
Output
0 Venice 1 Frankfurt 2 London
Conclusion
With this, I wish you happy coding without IndexError: tuple index out of range!
Leave a comment