One of the most common types of error that can occur in Python is an index error. When the program is trying to access an undefined non-existing index or element in a list of Python then the system gives an index error.
Contents
Fix Index Error: List Index Out Of Range
It is some kind of a bug in the program you enter and we can easily remove it by doing a little bit of debugging. The only way to avoid this error is to use the indexes of the list program properly.
There are two common ways in which “list index out of range” error can occur,
- When during iteration you forget that the list is indexed from zero.
- When you do not use range() during a list’s iteration.
List indexed from zero
Here is a simple example of how this error occurs and how we can resolve it:
# Declaring list list_fruits = ['Apple', 'Banana', 'Strawberry'] # Print value of list at index 3 print(list_fruits[3])
Output:
Traceback (most recent call last):
File “list-index.py”, line 2, in <module>
print(list_fruits[3]);
IndexError: list index out of range
In this example, there is a list named “list_fruits” with three values apple, banana, and strawberry. And we are trying to print the value at the index[3] in the program.
We know that the index of a list starts from 0, so the apple got index 0, banana got index value 1, and strawberry got index value 2, not 3.
Due to this reason, when you try to print the value at index[3] it will give an index error. Now, the correct program in Python will be like:
# Declaring list list_fruits = ['Apple', 'Banana', 'Strawberry'] # Print value of list at index 1 print(list_fruits[1])
Output:
Banana
Now, we take the example of a “while” loop. In this example, we have used a program in Python for a while loop. This loop prints out the value from the “list_fruits” at the index position stored in count (i=0). Then, it adds 1 to the “count” variable.
This loop continues until the value of “count” is no longer less than or equal to the length of the “list_fruits”.the error occurs in line 5, as shown in output where print(list_fruits[i]) means that the value of “i” exceeds the index value of list “list_fruits”
# Declaring list list_fruits = ['Apple', 'Banana', 'Strawberry'] i=0 # while loop less than and equal to list "list_fruits" length. while i <= len(list_fruits): print(list_fruits[i])
Output:
Apple
Banana
Strawberry
Traceback (most recent call last):
File “list-index-while.py”, line 5, in <module>
print(list_fruits[i])
IndexError: list index out of range
To solve this issue, we have to put correct values on the list. The solution is given like:
# declaring list list_fruits = ['Apple', 'Banana', 'Strawberry'] i=0 # while loop less than list "list_fruits" length while i <len(list_fruits): # print the value of i print(i) # print value of list print(list_fruits[i]) i += 1
Output:
0
Apple
1
Banana
2
Strawberry
We’ve successfully solved the error and now our loop is no longer trying to print out the wrong output.
Read more: What is Fix “Invalid Character Identifier” in Python Programming?
For loop to use range ():
When you’re iterating over a list of numbers, it’s easy to forget to include range (). Let’s see an example of this error in which the program does not run well:
Ages= [10,11,12] Forage in ages: print(age[ages]) To solve this issue we have to add a range() to iterate through the list of ages. Ages = [10,11,12] Forage in range(0, len(ages)): print(age[ages])
Output:
- 10
- 11
- 12
All of the values from the “ages” array are printed and the range() statement creates a list of numbers. The numbers (0,1,2) can then be used to access values in “ages” by their index number.