How to Resolve IndexError- List Index Out of Range in Python?

Do you know why does the List Index Out of Range Occur?

The simple answer to the above query is that it occurs because you attempted to retrieve an index element that was not present in the list or was out of range; in other words, the index you were attempting to get was empty.

For Example:

# Give the list as static input and store it in a variable.
gvn_lst = ["hello", "this", "is", "btechgeeks"]
# Printing the element present at the index 4
print(gvn_lst[4])

Output:

IndexError Traceback (most recent call last)
<ipython-input-8-8c8b7287163d> in <module>()
2 gvn_lst = ["hello", "this", "is", "btechgeeks"]
3 # Printing the element present at the index 4
----> 4 print(gvn_lst[4])

IndexError: list index out of range

Explanation:

The first thing to remember is that list indexing begins at 0 and ends at n-1.

Here there are only 4 elements in the given list, but when you try to access the element at index 4 (5th element ) it raises an IndexError because there is no 5th element present which is out of range.

The answer to this problem is to avoid fetching elements whose index does not exist.

In For Loops

# Give the list as static input and store it in a variable.
gvn_lst = ["hello", "this", "is", "btechgeeks"]
# Loop till the length of the given list+1 using the for loop
for itr in range(len(gvn_lst)+1):
    # Print the element present at the iterator value in the given list
    print(gvn_lst[itr])

Output:

hello
this
is
btechgeeks
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-13-20aaca6cb04b> in <module>()
4 for itr in range(len(gvn_lst)+1):
5 # Print the element present at the iterator value in the given list
----> 6 print(gvn_lst[itr])

IndexError: list index out of range

Explanation:

You may be wondering what the error in the above code. Once you have a basic knowledge of the list and its index, the error is as simple and clear as the previous one. Because list indexing begins at 0 and finishes at n-1, we used a for loop with range function to try to retrieve all the elements, and it starts the for loop at 0 instead of 1 and ends at n+1, here n=4, thus it is attempting to fetch the element at index 5 which has no value, thus returning Indexerror.

n= number of elements

Solution:

We can stop iterating the for loop before the list runs out of range.  To accomplish this, we must include a condition inside the range function that tells it to stop before attempting to fetch an index that is out of range.

# Give the list as static input and store it in a variable.
gvn_lst = ["hello", "this", "is", "btechgeeks"]
# Loop till the length of the given list using the for loop
for itr in range(len(gvn_lst)):
    # Print the element present at the iterator value in the given list
    print(gvn_lst[itr])

Output:

hello
this
is
btechgeeks

In While Loops

When using a while loop, the same Indexerror can occur; the cause may be different, but the error happens for the same reason: we were attempting to fetch an index that was not present in the list.

# Give the list as static input and store it in a variable.
gvn_lst = ["hello", "this", "is", "btechgeeks"]
# Take a variable and initialize its value with 0
k=0
# Loop until the k value is less than or equal to length of the given list 
# using the while loop
while k<=len(gvn_lst):
    # Print the element present at the kth index in the given list
    print(gvn_lst[k])
    # Increment the value of k by 1
    k+=1

Output:

hello
this
is
btechgeeks
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-15-c2bc2cb0790c> in <module>()
7 while k<=len(gvn_lst):
8 # Print the element present at the kth index in the given list
----> 9 print(gvn_lst[k])
10 # Increment the value of k by 1
11 k+=1

IndexError: list index out of range

Explanation:

Where is the Error present here?

Here the error occurs due to the operator. Observe that <= is used, and k begins at 0 and ends at the length of the given list, which is 4. To get to the point, it goes from index 0 to index 4, with 5 elements in between, and the above list only has 4 elements, with no element at index 5.

Solution:

Use instead of = to prevent the problem when it tries to get the 5th element as well because len(gvn_lst) returns 4. When less than is used, the index is only compared up to 4, not four. As a result, the error has been resolved.

# Give the list as static input and store it in a variable.
gvn_lst = ["hello", "this", "is", "btechgeeks"]
# Take a variable and initialize its value with 0
k=0
# Loop until the k value is less than to length of the given list 
# using the while loop
while k<len(gvn_lst):
    # Print the element present at the kth index in the given list
    print(gvn_lst[k])
    # Increment the value of k by 1
    k+=1

Output:

hello
this
is
btechgeeks