IndexError: list index out of range

In python, the IndexError: list index out of range error occurs when you are trying to access indices which are not valid in the list. When you try to access an undefined element from the list, python interpreter throws this “IndexError: list index out of range” error.

The python objects such as list, tuple and string contains ordered collection of element . Each element is identified by index. If you are trying to access an item beyond the maximum index value, the list can not identify the element. In this case, python throws “IndexError: list index out of range” error.

In Python, the “IndexError: list index out of range” error occurs when you are trying to access an undefined element beyond the list index. The list index is used to identify the elements in the list. This error occurs when you have access beyond the allowed list index.

Through this article, we can see what this error is, how this error can be solved. The error would be thrown as like below.

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


Different Variation of the error

Various errors will be shown based on python objects such as list, tuple, and string. The different error messages are shown as below.

IndexError: list index out of range
IndexError: tuple index out of range
IndexError: string index out of range


Root Cause

In python, if an undefined element is tried using an index beyond the allowed index value in objects such as list, tuple, and string, the python interpreter can not identify the element from the index. In this case, the python interpreter will throw an error called “IndexError: list index out of range”

Based on the type of object used, it throws various error messages such as “IndexError: tuple index out of range” and “IndexError: string index out of range.” 



How to reproduce this error

If an undefined element is tried to access using an index beyond the allowed limit, python interpreter will throw this error message. In the example below, the list contains five elements and index value is from 0 to 4. If you try to access the element in index 5. python will throw the error “IndexError: list index out of range”.

a = [2,4,6,8,10]
print (a[5])

Output

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


Forward index of the list

Python supports two indexing types, forward indexing and backward indexing. The forward index will start at 0 and end with the number of elements in the list. The forward index is used to iterate a element in the forward direction. The element in the list is printed in the same index sequence. The index value is increased to the next index value.

index01234
valueabcde


Backward index of the list

Python is supporting backward indexing. The back index starts from-1 and the index ends with the negative value of the number of elements in the list. The backward index is used to iterate the elements in the backward direction. The element in the list is printed in the reverse sequence of the index. The index value is decremented to get the next index value. The back index is as shown below

Index-5-4-3-2-1
Valueabcde


Solution 1

The index value should be within the range of the allowed index value. Normally, the list contains index starting from 0 to the length of the list. The negative index value starting from -1 will point to the last index of the list.

a = [2,4,6,8,10]
print (a[3])

Output

8
[Finished in 0.1s]


Solution 2

The list is dynamically created in real time. The list is iterated and the elements are retrieved based on the index. In that case , the value of the index is unpredictable. If the element in the list is retrieved by an index, the index value should be validated with the length of the list.

a = [2,4,6,8,10]
index = 3
if index < len(a) :
	print a[index]

Output

8
[Finished in 0.1s]


Solution 3

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

a = [2,4,6,8,10]
for item in a:
	print item

Output

2
4
6
8
10
[Finished in 0.0s]



Related Articles

Leave a Reply

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