Python indexerror: list index out of range Solution

Posted in /  

Python indexerror: list index out of range Solution
vinaykhatri

Vinay Khatri
Last updated on November 24, 2024

    Indexerror: list index out of range is one of the most common errors you may encounter while dealing with Python lists . It is a Python runtime error that occurs when you try to access an element from the Python list using an index value that is out of the Python list indices range.

    In this Python tutorial, we will walk through the Indexerror: list index out of range error and discuss why it occurs and how to solve it. So let's begin with the Error Problem statement.

    The Problem: indexerror: list index out of range

    Whenever you encounter the indexerror: list index out of range statement on your terminal or command prompt while executing a Python program, this means you are trying to pass an index value in the square bracket that exceeds the index range of the defined list. If we initialize a list object in Python, the index range of that list goes from 0 to n-1 where n is the total number of elements present in the list.

    For example, if a list has 4 elements, then it will have an index range from 0 to 3 where 0 will represent the first element index value and 3 will represent the last or 4th element index value.

    names = ["Rahul", "Jhon", "Raj", "Anil"]
    1. The above list names has 4 elements, so its index value will range from 0 to 3 .
    2. Python list also supports negative indexing range from -1 to -n where -1 is the index value of the last element and -n the first element's index value. Tak negative index number also into consideration. The above names list also has an index value range from -1 to -4 .

    If we consider both points, we can either use index value from 0 to 3 or -1 to -4 to access the elements of the names list.

    Example

    names = ["Rahul", "Jhon", "Raj", "Anil"]
    
    # access first element using positive index
    print(names[0])                               #Rahul
    
    # access first element using negetive index
    print(names[-4])                               #Rahul

    But if we try to pass an index value that does not lies between -4 to -1 and 0 to 3 , we will receive the index error " list index out of range ".

    Reason

    A list object support indexing to access its elements, and a Python list support positive as well as negative indexing. In positive indexing, the value of the index number starts from 0 up to n-1 , and in negative indexing, the index number starts from -1 upto -n . And when we want to access the elements from the list, we are only allowed to specify the index value between the -n to n-1 (included). But if we pass an index value less than -n and greater than n-1 by mistake, we will receive the indexerror: list index out of range .

    Example 1

    names = ["Rahul", "Jhon", "Raj", "Anil"]
    
    # access element by passing index value less than -n
    print(names[-5])

    Output

    Traceback (most recent call last):
    File "main.py", line 4, in <module>
    print(names[-5])
    IndexError: list index out of range

    Example 2

    names = ["Rahul", "Jhon", "Raj", "Anil"]
    
    # access element by passing index value greater than n-1
    print(names[4])

    Output

    Traceback (most recent call last):
    File "main.py", line 4, in <module>
    print(names[4])
    IndexError: list index out of range

    Solution

    To solve this problem, you need to make sure that the index value you are passing must be greater than or equal to -n and smaller than and equal to n-1 . If you only use a positive index number to access the elements from a list, then you need to make sure that you are passing the index value from 0 to n-1 . Now let's solve the above two examples

    Solution Example 1

    names = ["Rahul", "Jhon", "Raj", "Anil"]
    
    print(names[-4])   #Rahul

    Solution Example 2

    names = ["Rahul", "Jhon", "Raj", "Anil"]
    
    print(names[3])    # Anil

    Conclusion

    In this Python tutorial, we discussed the Python IndexError: list index out of range, and its solution. The most common scenario where you can encounter this error is when you forget to pass the range() function or mishandle the calculation of the while loop and index number inside the while loop. This error is very common, so while dealing with loop and list index together, be extra careful and compute your calculation for index values very precisely.

    If you are still stuck with the error, feel free to comment down your code and message, and we will try to help you to debug it.

    People are also reading:

    Leave a Comment on this Post

    0 Comments