How to debug the list index out of range python error?
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.
Table of Contents- list index out of range python
Why do we get the list index out of range Python error?
If 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 for a new programmer debugging this error, it could be tricky
So let us first understand how indexes work and then look at the solutions for the list index out of range error.
Lists are used to store multiple values in a single variable. Once stored, each value is given a unique position, these positions are called indexes. These indexes are used to access a particular value in a list. Furthermore, Python lists are 0-indexed, which means the index of the first value is 0 and the last index is n-1.
Let me explain this with an example, consider a list
["Hire", "top", "freelancers"], the index of
x respectively. And trying to return
x is when you receive a List index out of range error in Python.
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, most new programmers make the mistake of hard coding the number of iterations. This is not a good practice as adding or removing an item from the list would break your loop.
To foolproof your loop of such instances, the
range() function along with
len() would return the length of the list dynamically. Subsequently preventing you from receiving the ‘list index out of range’ error in python.
range() function returns a sequence of numbers starting from 0, and ends at the integer passed as a parameter. We use this method along with the
len() function. This function returns the length of the parameter passed. This way no matter the length of the list, we would not go out of range.
We pass the return value of the
len() function as the parameter for the
Syntax of range()
range(start, stop, step)
start - Optional, an integer that indicates the start of the sequence. In case it’s 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
We pass the return value of the
len() function as the stop parameter for the
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])
Through this method, the loop runs from 0 right up to 5 i.e, the length of the list passed into the
len() function. This was to avoid the ‘list index out of range’ error.
Closing thoughts - list index out of range python
Although the above method solves the ‘list index out of range’ error, another caveat while dealing with list indexes is as follows. Programmers often use the wrong variable to index a list. This example would give you a better understanding.
list1 = [5,10,15,20,25] for i in list1: print(list1[i])
Note that this code is iterating over the values in the list. Here i iterates over the value in the list and in the first iteration,
list1[i] refers to the 5th index. Subsequently, the 10th index is referenced in the second iteration, and since the list only consists of 5 values python returns the ‘index out of range’ error.
This is another case that you should be aware of to avoid the error