IndexError: list index out of range error occurs when we try to access the list elements using indexing greater than the list’s length. For example, suppose a list has three elements and you are trying to access an element of the list using index value10. In that case, we will get the IndexError: list index out of range error because we are accessing the element using an index that is not in the range of the list.
How to Solve IndexError: list index out of range?
The error IndexError: list index out of range occurs when we try to access the element from the list using an index value that is greater than the range of the list. In simple words, we are trying to access the element from the list that is not actually in the range of the list.
The IndexError usually refers to the sequential data types where different elements are arranged in proper sequence. Let us assume that we have a list and the length of the list is n. That means the last element of the list can be accessed by n-1 because the indexing starts from 0.
The first element of the list can be accessed by index value 0. And the last element of the list can be accessed by index value n-1 where n is equal to the length of the list.
# creating a list
list1 = [1,2,3,4,5,6,7]
# index value
list1[7]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
/tmp/ipykernel_373781/2432924903.py in <module>
3
4 # index value
----> 5 list1[7]
IndexError: list index out of range
As you can notice we got the error because we have used an index value that is not in the range of the list.
Using range() method
In general, we can fix the error by making sure that we are not going out of range. We can do this by using the range function and providing the length of the list: The range function starts from 0 ( if not specified) and ends at the specified range.
# creating a list
list1 = [1,2,3,4,5,6,7]
# using range function
for i in range(len(list1)):
print(i)
0
1
2
3
4
5
6
Notice that the output shows the index values that can be used to access the elements from the list. If we will use any index value except those shown above, we will get IndexError: list index out of range error.
To access the elements, we can use the following code where we are accessing the elements using the above-given index values
# creating a list
list1 = [1,2,3,4,5,6,7]
# using range function
for i in range(len(list1)):
print(list1[i])
1
2
3
4
5
6
7
Notice that we got access to the elements using the index values:
Using a while loop
In Python, loops are used to iterate through an iterable object. The list is one of the iterable in Python. We can use a while loop to iterate through it and access the elements:
Let us assume that we have a list and we want to access all the elements using a while loop. Here, we will first, indicate the common error that most people make:
# creating a list
list1 = ['a', 'b', 'c', 'd', 'e']
# strating index
i = 0
# using while loop
while i<=len(list1):
print(list1[i])
i+=1
a
b
c
d
e
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
/tmp/ipykernel_373781/2103807773.py in <module>
7 # using while loop
8 while i<=len(list1):
----> 9 print(list1[i])
10
11 i+=1
IndexError: list index out of range
Although we got access to all the elements in the list still getting an error because we are going out of range: To solve the issue we have to subtract 1 from the length of the list. The reason for subtracting 1 is that the indexing starts from 0 which means the first element is accessed with an index value of 0, the second element can be accessed with an index value of 1, and so on. The last element can be accessed with an index value n-1, which is why we need to subtract 1 from the length of the list:
# creating a list
list1 = ['a', 'b', 'c', 'd', 'e']
# strating index
i = 0
# using while loop
while i<=len(list1)-1:
print(list1[i])
i+=1
a
b
c
d
e
Alternatively, we can also change the greater or less than value to just greater than instead of subtracting one from the length:
# creating a list
list1 = ['a', 'b', 'c', 'd', 'e']
# strating index
i = 0
# using while loop
while i<len(list1):
print(list1[i])
i+=1
a
b
c
d
e
As you can see we are no longer getting IndexError: list index out of range error.
Using negative indexing
Python has a cool feature which is we can also use negative indexing to access the elements from the list. The negative indexing starts from -1 which represents the last element, -2 which corresponds to the second last element, and so on.
We can access the last element of the list using negative indexing as shown below:
# creating a list
list1 = ['a', 'b', 'c', 'd', 'e']
# negative indexing
list1[-1]
e
We can also use a while loop with negative indexing which will get access to the elements in reverse order:
# creating a list
list1 = ['a', 'b', 'c', 'd', 'e']
# strating index
i = -1
# using while loop
while i>=0-len(list1):
print(list1[i])
i-=1
e
d
c
b
a
Notice that the elements are being accessed in reverse order:
Using slicing of the list
Another cool feature in Python is slicing. We can access multiple elements from the list using the slicing method. For example, let us say that we want to print only the second and third element from the list:
Here is how you can do that:
# creating a list
list1 = ['a', 'b', 'c', 'd', 'e']
# slicing of the list
list1[1:3]
['b', 'c']
Slicing actually returns a sublist from the main list.
If you want to access the last two elements you can access them using any of the following two methods:
# creating a list
list1 = ['a', 'b', 'c', 'd', 'e']
# slicing of the list
list1[3:]
['d', 'e']
As you can see, we got the last elements: One feature of slicing is that even if we access elements out of range, we will not get IndexError: list index out of range error. For example, the length of our list is 5 and we want to access elements from 3 up to 30:
# creating a list
list1 = ['a', 'b', 'c', 'd', 'e']
# slicing of the list
list1[3:30]
['d', 'e']
Notice that the code does not return IndexError because in slicing we don’t get this error anymore even if the range is out of the index. You only get the error when you are trying to access individual elements based on indexing.