If you use Python, you may have encountered the IndexError
error in response to some code you've written. The IndexError
message in Python is a runtime error. To understand what it is and how to fix it, you must first understand what an index is. A Python list (or array or dictionary) has an index. The index of an item is its position within a list. To access an item in a list, you use its index. For instance, consider this Python list of fruits:
fruits = ["apple", "banana", "orange", "pear", "grapes", "watermelon"]
This list's range is 5, because an index in Python starts at 0.
- apple: 0
- banana: 1
- orange: 2
- pear: 3
- grapes: 4
- watermelon: 5
Suppose you need to print the fruit name pear
from this list. You can use a simple print
statement, along with the list name and the index of the item you want to print:
>>> fruits = ["apple", "banana", "orange", "pear", "grapes", "watermelon"]
>>> print(fruits[3])
pear
What causes an IndexError in Python?
What if you use an index number outside the range of the list? For example, try to print the index number 6 (which doesn't exist):
>>> fruits = ["apple", "banana", "orange", "pear", "grapes", "watermelon"]
>>> print(fruits[6])
Traceback (most recent call last):
File "", line 2, in
IndexError: list index out of range
As expected, you get IndexError: list index out of range
in response.
How to fix IndexError in Python
The only solution to fix the IndexError: list index out of range
error is to ensure that the item you access from a list is within the range of the list. You can accomplish this by using the range()
an len()
functions.
The range()
function outputs sequential numbers, starting with 0 by default, and stopping at the number before the specified value:
>>> n = range(6)
>>> for i in n:
print(i)
0
1
2
3
4
5
5
The len()
function, in the context of a list, returns the number of items in the list:
>>> fruits = ["apple", "banana", "orange", "pear", "grapes", "watermelon"]
>>> print(len(fruits))
6
List index out of range
By using range()
and len()
together, you can prevent index errors. The len()
function returns the length of the list (6, in this example.) Using that length with range()
becomes range(6)
, which returns items at index 0, 1, 2, 3, 4, and 5.
fruits = ["apple", "banana", "orange", "pear", "grapes", "watermelon"]
for i in range(len(fruits)):
print(fruits[i])
apple
banana
orange
pear
grapes
watermelon
Fix IndexError in Python loops
If you're not careful, index errors can happen in Python loops. Consider this loop:
>>> fruits = ["apple", "banana", "orange", "pear", "grapes", "watermelon"]
>>> n = 0
>>> while n <= len(fruits)
print(fruits[n])
n+=1
apple
banana
orange
pear
grapes
watermelon
Traceback (most recent call last):
File "", line 4, in
IndexError: list index out of range
The logic seems reasonable. You've defined n
as a counter variable, and you've set the loop to occur until it equals the length of the list. The length of the list is 6, but its range is 5 (because Python starts its index
at 0). The condition of the loop is n <= 6
, and so thewhile
loop stops when the value of n
is equal to 6:
- When n is 0 => apple
- When n is 1 => banana
- When n is 2 => orange
- When n is 3 => pear
- When n is 4 => grapes
- When n is 5 => watermelon
- When n is 6 => IndexError: list index out of range
When n
is equal to 6, Python produces an IndexError: list index out of range
error.
Solution
To avoid this error within Python loops, use only the < ("less than") operator, stopping the while
loop at the last index of the list. This is one number short of the list's length:
>>> fruits = ["apple", "banana", "orange", "pear", "grapes", "watermelon"]
>>> n = 0
>>> while n < len(fruits)
print(fruits[n])
n+=1
apple
banana
orange
pear
grapes
watermelon
There's another way to fix, this too, but I leave that to you to discover.
No more Python index errors
The ultimate cause of IndexError
is an attempt to access an item that doesn't exist within a data structure. Using the range()
and len()
functions is one solution, and of course keep in mind that Python starts counting at 0, not 1.
1 Comment