The python error IndexError: list index out of range occurs when an incorrect indices 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.
The common reason for the exception IndexError: list index out of range is that
- The list is iterated with index starting from 1 instead of 0.
- The last item is accessed with the length of the list. It is expected to be Len(list)-1.
- The item in the list is deleted when iterating the list. If an element is deleted, the index value will be reduced by one.
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 list, such as tuple, array, string, uses the index to identify the element in the list. Each index contains an element of a list. If an incorrect index is used to locate an element in a list, the list index out of range exception will be thrown. 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.
index | 0 | 1 | 2 | 3 | 4 |
value | a | b | c | d | e |
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 |
Value | a | b | c | d | e |
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.
The list in this example contains 5 elements. Index values start at 0 and the last index value is 4. Python allows the list to be accessed in reverse order. The reverse index will begin with -5 and will end with -1. If the index value is different from the value listed, the list index out of range exception will be thrown.
a = [2,4,6,8,10]
print (a[3])
Output
8
[Finished in 0.1s]
Solution 2 – Using len() function
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 – for loop with ‘in’
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]
Solution 4 – for loop with range()
The range function should be used to iterate the items in the list. the range will give the values starting from 0 and ends with value less one. The list index starts with 0. The range function will help in iterating items in the for loop.
a = [2,4,6,8,10]
for i in range(len(a)):
print a[i]
Output
2
4
6
8
10
[Finished in 0.1s]
Solution 5 – for loop with reversed()
If a list is iterated to delete an item in the list, the list index out of range exception will be thrown. If an item is deleted in the list, the index values will be reduced by one. The last index does not contain item. If a list is iterated in reversed order to delete an item, the index will not be changed for the iterating index values. The example below will show how to delete a item from a list.
a = [2,4,6,8,10]
for i in reversed(range(len(a))):
if i==3 :
a.pop(i)
for i in reversed(range(len(a))):
print(a[i])
Output
10
6
4
2
[Finished in 0.1s]