How to debug list index out of range error in python?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
- Why do we get the list index out of range error?
- Solution using range() and len()
- Closing thoughts
- Other Related Concepts
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, x and x respectively. And trying to return x 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 rannge()
Parametersstart - 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:
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 ThoughtsAlthough 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..
Syntax of index():
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.