Resolve IndexError: list index out of range in Python

In this tutorial, we will be learning something about runtime error – List index out of range in Python. You know what, you can easily resolve it, but first of all you must know Why it occurs?

The simple answer to the above question is it occurs because you were trying to access an index element that is not present in the list or that is out of range, in simple words, we can say the index you were trying to fetch is empty.

Let’s begin with a simple example to clear your doubts regarding IndexError:

fruits=['apple','banana','grapes']
print(fruits[3])
OUTPUT:
IndexError: list index out of range

Consider the list of fruits just created above, which consists of 3 elements. The very first thing, we need to keep in mind is list indexing starts from 0 and ends at n-1. In this case, we were trying to fetch 3rd element which actually has an index of 2. The solution to the above problem is try not to fetch elements whose index doesn’t exist.

Index error in for loop

Sometimes, you might encounter IndexError in for loop mostly in range() function.

fruits=['apple','banana','grapes']
for i in range(len(fruits)+1):
    print(fruits[i])
OUTPUT:
apple
banana
grapes
IndexError: list index out of range

You might be thinking what’s the error in the above code. The error is again as simple as the previous one, once you get a basic understanding about the list and its index. As list indexing starts from 0 and ends at n-1, we have used a for loop with range function which is trying to fetch all the elements and it starts for loop from 0 instead of 1 and ends at n+1, here n=3, so it is trying to fetch the element at index 4 which has no value, hence returning Indexerror.

How to resolve?

Simply we can stop the for loop iterating before the list runs out of range. For doing so, we need to give the condition inside the range function to stop before it tries to fetch the index out of range.

fruits=['apple','banana','grapes']
for i in range(len(fruits)):
    print(fruits[i])

Index error in while loop

The same Indexerror can occur when using a while loop, the cause may be different but the error occurs because of the same reason, we were trying to fetch the index which is not present in the list. Let’s move to the example for a better understanding :

fruits=['apple','banana','grapes']
i=0
while i<=len(fruits):
    print(fruits[i])
    i+=1

The above code looks perfect with no error, it should run smoothly, right.

But here is the output.

OUTPUT:
apple
banana
grapes
IndexError: list index out of range

The confusion is very obvious, Where is the error?

The error is in the operator, yes the operator. As you can see, <= is used and i starts from 0 and it ends at the length of fruits which is 3. Coming to the point, it starts from index 0 up to index 3, which has 4 elements in between, and the above list has only 3 three elements, no element at index 4 is present.

How to resolve?

Use < instead of <=, so as to avoid the situation where it tries to fetch the 4th element as well because len(fruits) return 3. When less than is used it only compares index up to 3, not 4. So here, the error is resolved.

Prevention

Don’t use list index to loop over the list. Try to access the elements directly as:

fruits=['apple','banana','grapes']
for fruit in fruits:
    print(fruit)
OUTPUT:
apple
banana
grapes

This resolves all the problems regarding the index as we are accessing the element directly.

Conclusion

Here, we have just completed a tutorial on Indexerror, basic understanding about it, why it occurs, how to resolve and also how can we prevent Indexerror.

Thank you for reading! Hope you enjoyed learning. Feel free to comment and share your reviews on the tutorial.

Leave a Reply

Your email address will not be published.