Register Login

Indexerror: list Index Out of Range in Python

Updated Jun 22, 2021

Python List Index Out of Range

If you are working with lists in Python, you have to know the index of the list elements. This will help you access them and perform operations on them such as printing them or looping through the elements. But in case you mention an index in your code that is outside the range of the list, you will encounter an IndexError.

List index out of range” error occurs in Python when we try to access an undefined element from the list.

The only way to avoid this error is to mention the indexes of list elements properly.

Example: 

# Declaring list
list_fruits = ['apple', 'banana', 'orange']
# 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 the above example, we have created a list named “list_fruits” with three values apple, banana, and orange. Here we are trying to print the value at the index [3].

And we know that the index of a list starts from 0 that’s why in the list, the last index is 2, not 3

Due to which if we try to print the value at index [3] it will give an error.

Indexerror: list Index Out of Range in Python

Correct Example: 

# Declaring list
list_fruits = ['Apple', 'Banana', 'Orange']
# Print list element at index 2
print(list_fruits[2]);

Output: 

Orange

1. Example with "while" Loop

# Declaring list
list_fruits = ['Apple', 'Banana', 'Orange']

i=0
# while loop less then and equal to list "list_fruits" length.
while i <= len(list_fruits):
    print(list_fruits[i])
    i += 1


Output:

Apple
Banana
Orange
Traceback (most recent call last):
  File "list-index-while.py", line 5, in <module>
    print(list_fruits[i])
IndexError: list index out of range

In the above case, the error occurs inline 5, as shown in output where print(list_fruits[i]) means that the value of “i” exceeds the index value of list “list_fruits.”

If you need to check why this error occurs, print the value of “i” just before “print(list_fruits[i])” statement.

print(list_fruits[i])

Example

# declaring list
list_fruits = ['Apple', 'Banana', 'Orange']

i=0
# while loop less then and equal to 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
Orange
3
Traceback (most recent call last):
  File "list-index-while.py", line 9, in <module>
    print(list_fruits[i])
IndexError: list index out of rang

In the above example output, we can see that the value of ‘i’ goes to “3”, whereas our list index is only up to 2.

Solution for this error 

# declaring list
list_fruits = ['Apple', 'Banana', 'Orange']

i=0
# while loop less then 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
Orange

2. Example with "for" Loop:

# declaring list

list_fruits = ['Apple', 'Banana', 'Orange']

# for loop to print the index from 0 to 3

for i in range(0,4):
    # print the value of i
    print(i)
    # print value of list
print(list_fruits[i])


Output:

0
Apple
1
Banana
2
Orange
3
Traceback (most recent call last):
  File "list-index-for.py", line 9, in <module>
    print(list_fruits[i])
IndexError: list index out of range

In the above example, we are printing the value at index 3, but the out list has indexed only up to 2.

To avoid such type of error, we have to run for loop in the range of “list” length.

Solution:

# declaring list
list_fruits = ['Apple', 'Banana', 'Orange']

# for loop to print the index in the range of list length
for i in range(len(list_fruits)):
    # print the value of i
    print(i)
    # print value of list
print(list_fruits[i])

Output:

0
Apple
1
Banana
2
Orange

Conclusion

We have seen the different situations where the list index out of range error might occur. You can check the length of the list using before performing operations or using the indexes.


×