Online Tutorials & Training Materials | STechies.com
Register Login

IndexError: tuple index out of range

29 Jan 2020 2:05 pm || 0

IndexError: tuple index out of range

Tuples in Python are a series of objects that are immutable. They are like lists. The elements of a tuple are accessed the same way a list element is accessed – by mentioning indices. But when using tuples you might have encountered "IndexError: tuple index out of range". This happens when you are trying to access an element that is out of bounds of the tuple.

The way to solve this error is by mentioning the correct index. Let us look a little more closely at this error and its solution.

Examples of IndexError: tuple index out of range

Take a look at this piece of code below:

# Declare tuple
tup = ('Apple', "Banana", "Orange")

# Print tuple value at index 10
print(tup[10])

Output:

File "pyprogram.py", line 5, in <module>
print(tup[10])
IndexError: tuple index out of range

As tuple has only 3 index and we are trying to print the value at index 10

Tuple Index 

Solution:

# Declare tuple
tup = ('Apple', "Banana", "Orange")

print(tup[0])
print(tup[1])
print(tup[2])

In the code above, there is a tuple called tup having three elements. So the index value starts from 0 and ends at 2. A print() method is called on the tuple to print out all of its elements.

The solution code executes successfully as the indices mentioned in the print() are 0,1 and 2. These indices are all within the range of the tuple, so the IndexError: tuple index out of range error is avoided.

IndexError: tuple index out of range

Example with While Loop

# Declare tuple
tup = ('Apple', "Banana", "Orange")

print('Print length of Tuple: ',len(tup))

i=0

# While loop less then and equal to tuple "tup" length.
while i <= len(tup):
    print(tup[i])
    i += 1

Output:

  File "pyprogram.py", line 10
        print(tup[i])
            ^
SyntaxError: invalid character in identifier

len() function count length of tuple as "3" so while loop runs for 4 time starting from 0 because value of i is 0, due to which when our while loop print the value of tup["3"] it goes out of range because out tuple "tup" has only 3 elements.

Correct Code:

# Declare tuple 
tup = ('Apple', "Banana", "Orange")
i=0

print('Print length of Tuple: ',len(tup))

# While loop less than tuple "tup" length.
while i < len(tup):
    print(tup[i])
    i += 1

Output

Print length of Tuple:  3
Apple
Banana
Orange

The tuple called "tup" has 3 elements. So, the index starts from 0 and ends at 2. In the solution code, there is a variable i having a value of 0. This variable is used as an incrementor in the while loop. The loop checks whether i is less than length of the tuple. Then, it prints out the element in the ith index.

So, the loop runs 3 times starting from 0 and going up to 2, while i is incremented at every iteration. It stops iterating when i=4 and it is greater than the length of the tuple. Thus, the IndexError: tuple index out of range is avoided, as the code does not try to access the 4th element that is out of range.