DEV Community

loading...

How to debug list index out of range error in python?

hrishikesh1990
Co-founder & CTO at Flexiple & Remote Tools ($1mn+ revenue).
Originally published at flexiple.com ・3 min read

In this python tutorial, we look at why we receive the IndexError: List index out of range error in python, post which we look at solutions for the error.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts. However, in case you are here only for the solution use this link.

Table of Content

Why do we get the list index out of range error?

In case you have worked with lists before, the chances of you coming across the list index out of your range error is quite high. This error is quite common and if you are new to programming debugging this error without understanding its meaning could be quite tricky. So let us first look at indexes in python and then we shall look at the solutions for the list index out of range error.

Python lists data type store values and given lists are ordered each value stored has a unique index that can be used to get the relevant object. Furthermore, Python lists are 0-indexed, which means the index of the first value is 0 and the last is n-1.

Let me explain this with an example, consider a list x = ["Hire", "top", "freelancers"], the index of "Hire", "top" and "freelancers" is x[0], x[1] and x[2] respectively. And trying to return x[3] is when you receive a List index out of range error.

Now that we have understood indexing, let's look at more instances when we come across the list index out of range error.

Solution using range() and len():

While looping through a list, new programmers make the mistake of hard coding the number of times you would want a loop to run based on the length of the list. This is not a good practice as you could add or remove an item from the list and this would make your loop break. And for such cases the range() function along with len() would help you fail-proof your loop so that you do not receive the list index out of range error. The range() function returns a sequence of numbers starting from 0 and incrementing them by 1. Although these values can be changed by passing the relevant parameters and the len() methods returns the length of the argument passed.

Syntax of range()

range(start, stop, step)
Enter fullscreen mode Exit fullscreen mode

Parameters

start - Optional, an integer that indicates the start of the sequence, in case left empty 0 is the default value

stop - An integer that indicates where the sequence should stop

step - Optional, and used in case you would like to increment by a number greater than 1. The default value is 1

By using the range() in a for loop where the start is 0 and passing the length of the list using len() we can ensure that the list does not do out of range.

Code to fix list out of range error:

list1 = ["Hire", "the", "top", 10, "python","freelancers"]

for i in range(0,len(list1)):
    print(list1[i])
Enter fullscreen mode Exit fullscreen mode

Through this method the loop runs from 0 right up to 5 the length of the list which is passed using the len() function avoiding the list index out of range error.

Closing Thoughts

Although the above solution, solves the list index out of range error, another common occurrence is when they use the wrong variable for the index. The lower example would give you a better understanding.

list1 = [5,10,15,20,25]

for i in list1:
    print(list1[i])
Enter fullscreen mode Exit fullscreen mode

Note that this method is not iterating over the list itself and by list1[i] you are referring to the 5th index which in our case does not exist and this would again return a list index out of range error. So using the above-mentioned solution would be the best bet to avoid the list index out-of-range error.

Discussion (0)