How to Solve IndexError: list index out of range in Python

The IndexError: list index out of range error occurs in Python because we call an index greater than the length of the list. The list is used for storing a series of values. Each element is assigned a unique index. We can access the element by using the index.

Values: 5 7 9 11 13 17 19 77
Index: 0 1 2 3 4 5 6 7

 

This is how the index and the value will be assigned to the list.

From the above list:

list = [5, 7, 9, 11, 13, 17, 19, 77]
print(list[0]) # The output is displayed as 5
print(list[2]) # The output is displayed as 9
print(list[1]) # The output is displayed as 7
print(list[6]) # The output is displayed as 19
print(list[8])

Output

IndexError: list index out of range

From the table, we can see that we have assigned values till 7, but there is no value for 8. So if we try to print this, an error is raised with the error message: indexerror: list index out of range. 

To solve IndexError: list index out of range error in Python, check out all the list elements, and make sure that you are not printing a list element that does not exist.

lis = [5, 7, 9, 11, 13, 17, 19, 77]

for i in range(0, len(lis)):
  print(lis[i], end="\n")

Output

5
7
9
11
13
17
19
77

You can see the output of this program. In this for loop, we set the ending position as the length of the lis. So the length of the list will be 8. But the ending index will be 7. In for loop, the starting position is inclusive, and the ending position is exclusive, i.e., the ending value will not be considered. So if the length is 8 up to 7, it will be iterating.

There is another place where we have to be careful:

lis = [5, 7, 9, 11, 13, 17, 19, 77]

for i in lis:
  print(lis[i], end=" ")

Output

IndexError: list index out of range

We get the IndexError: list index out of range error because for i in lis will automatically iterate over the elements. And printing, i will directly print the elements. So we have to be careful while using for i in lis. The proper solution is:

lis = [5, 7, 9, 11, 13, 17, 19, 77]

for i in lis:
  print(i, end="")

Output

5 7 9 11 13 17 19 77

Solve IndexError using try-except statement in Python

The try-except statement is used to handle these errors within our code in Python. The try block is used to check some code for errors, except block will handle that error.

lis = [5, 7, 9, 11, 13, 17, 19, 77]

try:
  print(lis[0]) # The output is displayed as 5
  print(lis[2]) # The output is displayed as 9
  print(lis[1]) # The output is displayed as 7
  print(lis[6]) # The output is displayed as 19
  print(lis[8]) # Index out Of range.

except IndexError:
  print("Index out Of range")

Output

5
9
7
19
Index out Of range

Conclusion

IndexError occurs when the given index exceeds the length of the list. To overcome this, we have to check the given index value. First, we have to know the length of the list so that the error can be easily identified. Then, we can use the try and except block to handle the index error exception. The IndexError can be solved by knowing the list length.

That’s it for this tutorial.

Related posts

How to print error in try-except in Python

TypeError: list indices must be integers or slices, not str in Python

TypeError: Only size-1 arrays can be converted to Python scalars

Leave a Comment