Hello coders!! In this article, we will learn about python list index out of range error and will also learn how to resolve such errors. At first, we should understand what does this means? Python list index out of range arises when we try to access an invalid index in our Python list. Now, let us look into this topic deeply to have a better understanding.
Illustration of Python list index out of range with examples:
Example 1: python list index out of range with len()
color = ['red', 'blue', 'green', 'pink'] print(color[len(color)])
In this code snippet, we have created a list called color having four elements – red, blue, green, pink
In python, the indexing of the elements in a list starts from 0. So, the corresponding index of the elements are:
- red – 0
- blue – 1
- green – 2
- pink – 3
The length of the list is 4. So, when we try to access color[len(color)], we are accessing the element color, which does not exist and goes beyond the range of the list. As a result, the list index out of bounds error is displayed.
example 2: python list index out of range in loop
def check(x): for i in x: print (x[i]) lst = [1,2,3,4] check(lst)
Here, the list lst contains the value 1,2,3,4 having index 0,1,2,3 respectively.
When the loop iterates, the value of i is equal to the element and not its index.
So, when the value of i is 1( i.e. the first element), it displays the value x which is 2 and so on.
But when the value of i becomes 4, it tries to access the index 4, i.e. x, which becomes out of bound, thus displaying the error message.
How to avoid the python list index out of range error?
1)Lists are indexed from zero:
We need to remember that indexing in a python list starts from 0 and not 1. So, if we want to access the last element of a list, the statement should be written as lst[len-1] and not lst[len], where len is the number of elements present in the list.
So, the first example can be corrected as follows:
color = ['red', 'blue', 'green', 'pink'] print(color[len(color)-1])
Now that we have used color[len(color)-1], it accesses the element color, which is the last element of the list. So, the output is displayed without any error.
2)Use range() in loop:
When one is iterating through a list of numbers, then range() must be used to access the elements’ index. We one forgets to use it, instead of index, the element itself is accessed, which may give a list index out of range error.
So, in order to resolve the error of the second example, we have to do the following code:
def check(x): for i in range(0, len(x)): print (x[i]) lst = [1,2,3,4] check(lst)
Now, that we have used the range() function from 0 to the length of the list, instead of directly accessing the elements of the list, we access the index of the list, thus avoiding any sort of error.
Conclusion: Python List Index Out of Range
In day to day programming, list index out of range error is very common. We must make sure that we stay within the range of our list in order to avoid this problem. To avoid it we must check the length of the list and code accordingly.
However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.