IndexError: list index out of range

The error IndexError: list index out of range happens when an incorrect index is used to access a list element. The index value should not be out of range. Otherwise IndexError will be thrown. For example, if you try to access indices that are outside the index range in the list, python interpreter will throw the error IndexError: list index out of range.

Let’s look at an example where the error happens. If there are 3 elements in a python list the index will be from 0 to 2. The index always begins with the 0. The last index is the element total minus one. If there are 3 elements in the list the last index is 2. If an index value is out of this range, then it will throw the error.

The IndexError: list index out of range error occurs in Python when you attempt to access an unknown element outside the list index. The list index is used to identify the items in the list. This error occurs when access is outside the index range of the list.



Exception

The error will be thrown as like below. The IndexError is seen when the error in the list index occurs.

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]


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]


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. 



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 *