IndexError: tuple index out of range

The python error IndexError: tuple index out of range happens when an item in a tuple is accessed using invalid index. The access index should be within the tuple index range. The error will be thrown if the index is out of range in python. To solve this error, make sure the item you are searching for is accessed from a tuple

The tuple is an ordered collection of objects. The tuples are indexed starting at 0 up to the total number of elements length. You can reach the elements in the tuple using reverse indexing stating -1. If an element is accessed outside the permitted tuple index range, then the error IndexError: tuple index out of range is thrown.

Otherwise, at a specific index, you attempt to access an object but the object is not currently available in that index. In this case the index error tuple index out of range will be thrown by the python tuple.



Exception

The stack trace of the index error will appear as shown below. The stack trace will display the line this error is being thrown at.

Traceback (most recent call last):
  File "/Users/knatarajan2/Desktop/test.py", line 2, in <module>
    print a[5]
IndexError: tuple index out of range
[Finished in 0.0s with exit code 1]


How to reproduce this error

If an object is accessed beyond the permissible tuple range, the object can not be located at that location. Hence the error “IndexError: out-of-range tuple index” is thrown. The index ranges from 0 through to the total tuple items. In the example below, the index range is from 0 to 4 and reverse index is -5 to -1. The accessed index 5 is out of range of tuple.

test.py

a = ( 'A', 'B', 'C', 'D', 'E' )
print a[5]

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    print a[5]
IndexError: tuple index out of range
[Finished in 0.0s with exit code 1]


Root Cause

The tuple is an ordered collection of objects. The objects are managed using the indexes. The index values starts from 0 up to the total value count. The reverse index starts with -1 unto the total number of items negative. If an object is accessed using an index outside the range of a tuple, the error will be thrown.



Forward index of the tuple

Python allows two forms of indexation, forward indexing and backward indexing. The index forwards begins at 0 and finishes with the number of items in the tuple. The forward index is used to iterate a tuple in the forward direction.

Index01234
ValueABCDE


Backward index of the tuple

The reverse index starts from-1 and the index ends with the negative value of the number of elements in the tuple. The backward index is used in reverse direction to iterate the elements. The objects in the tuple is printed in the reverse sequence of the index. The reverse index shall be as shown below

Index-5-4-3-2-1
ValueABCDE


Solution 1

The index value should be within the permissible range of the index value. Typically, the tuple contains index beginning at 0 to the length of the tuple. The negative index value beginning at -1 would point to the tuple’s last index

test.py

a = ( 'A', 'B', 'C', 'D', 'E' )
print a[3]

Output

D
[Finished in 0.1s]


Solution 2

If the tuple is created dynamically, then the tuple length is unknown. The tuple is iterated and the elements are retrieved based on the index. The index value is uncertain in this situation. If an index retrieves the element in the tuple the index value should be validated with the tuple length.

a = ( 'A', 'B', 'C', 'D', 'E' )
index = 3
if index < len(a) :
	print a[index]

Output

D
[Finished in 0.1s]


Solution 3

The python membership operator is used to iterate all elements with in the tuple. The membership operator gets all the elements in the tuple without using the element index. The loop helps to iterate all the elements in the tuple. The example below shows how to use a membership in a loop. The error IndexError: tuple index out of range will be resolved by the membership operators.

test.py

a = ( 'A', 'B', 'C', 'D', 'E' )
for item in a:
	print item

Output

A
B
C
D
E
[Finished in 0.0s]



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *