Index Error in Python
The index errors are the run time error that is raised in Python when we try to access an index that does not exist. This might seem very trivial but they are among the most common error that occurs during programming. They are easy to be resolved and just require a little debugging to make the program error-free.
Here, we will create a scenario in which we may face this error while working with a list and try to resolve this error. Let us try to understand the error indexerror: list index out of range.
indexerror: list index out of range
To understand why this error occurs while using a list, we need to first understand the basics of indexing and how it works in the list?
Indexing can be defined as a way to refer to an individual element in the iterable that is based on the position of the individual element. In Python, objects including lists are zero-indexed which means the very first element of the iterable has zero “0” as its index and the subsequent elements have index incremented by one.
Now, let us try to understand this error by breaking the error message word by word.
indexerror: list index out of range
It is easy to determine that the program is attempting to access an index that does not exist or say it is not in the range of the index.
Example:
Items = ["Item1", "Item2", “Item3"]
Here, items list holds three items inside it and from what we have learned earlier in this module we can say that Item1 will have 0 as its index value while Item2 and Item3 will have 1,2 as their respective index values. Now, if we try to access the element using any other index value other than these values an indexerror will be returned.
There are two possible cases in which we can encounter the “list index out of range” error:
- When you lost the count of items on the list or you forget the list is zero-index.
- When we do not use range() to iterate over the list
Lists Are Zero-Indexed
The program below prints all the elements of the lists using the index value of the elements of the lists.
Items = ["Item1", "Item2", "Item3"] index = 0 while index <= len(Items): print(Items[index]) index += 1
Output:

Now, let us discuss what happened when we tried to execute this code. We have created the list that was discussed above and a new variable that we have initialised as zero. This index variable will be used to hold different index values of different elements of the list. We have used a while loop to check the flow of execution of the code. The code inside the while block will be executed if the value of the index is less than or equal to the length of the list and each time when this block will be executed the value of the index variable will be incremented by 1. To calculate the length we have used the len() method which returns the total number of elements that are present inside the list. This means len(items) will store 3 as the value and for the last time when the value of the index will be 3 and the condition will be true and the code inside will be implemented but throwing an error this time.
Debugging the Error
Items = ["Item1", "Item2", "Item3"] index = 0 while index < len(Items): print(Items[index]) index += 1
Output:

Debugging this error was quite simple as it can be done by removing the equal to “=” operator in this case. Once we remove the equal to operator when the index value will be 3 then the condition will become false and the code inside the while block won’t be executed.
Thus, we were successful in removing the error from our program.
Not Using range()
While dealing with the list of numbers it is quite possible to forget to use range() to define the extent of the loops while accessing the elements of the list that might raise errors while execution.
Let us understand this with an example:
Vehicle_Numbers = [1001, 1042, 9251] for Vehicle_Number in Vehicle_Numbers: print(Vehicle_Numbers[Vehicle_Number])
Output:

After executing the program this error is encountered, let’s see what happened, here Vehicle_number stored the actual value of the element of the list and not the index element of the list. So, when we used it directly the first value that was assigned to vehicle_number was 1001 and not 0, which caused this error.