Select Page

How to Solve Python IndexError: list index out of range

by | Programming, Python, Tips

When you want to access a value inside a list, you must use an index value in the range of the list. Using an index value out of range will raise the error: IndexError: list index out of range.

You can solve this index error by using the range function on the length of the list to get the index values. Alternatively, you can use a for… in loop over the list without indexing.

This tutorial will go through the error in detail and an example to learn how to solve it.


IndexError: list index out of range

What is an Index Error?

Python’s IndexError occurs when the index specified does not lie in the range of indices in the bounds of a list. In Python, index numbers start from 0 and end at n-1, where n is the number of elements present in the list. Let’s look at an example of a Python array:

particles = ["electron", "muon", "proton"]

This array contains three values, and the first element, electron, has an index value of 0. The second element, muon, has an index of 1. The third element, proton, has an index of 2.

If we try to access an item at index position 3, we will raise an IndexError, because the list range is 0 to 2.

print(particles[3])
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
1 print(particles[3])

IndexError: list index out of range

When accessing a list, remember that Python list indexing starts with 0.

Example: Iterating Over a List Using a For Loop

Let’s look at an example where we iterate over a list of numbers using a for loop:

numbers = [4, 32, 12, 7]

for number in numbers:

    print(numbers[number])

We want to print out all values inside the countries array to the console. Let’s run the code to get the output:

IndexError                                Traceback (most recent call last)
      3 for number in numbers:
      4 
      5     print(numbers[number])
      6 

IndexError: list index out of range

We can check why the error occurs by adding a print statement before accessing the list:

numbers = [4, 32, 12, 7]

for number in numbers:

    print(number)

    print(numbers[number])
4
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
      5     print(number)
      6 
      7     print(numbers[number])
      8 

IndexError: list index out of range

The program prints the first number, 4, to the console and uses this value as the index for the numbers list. The value assigned to number is not an index but a value in the list. When we try to use this value for indexing, we are performing:

print(numbers[4]

The list index only goes up to 3; accessing an element at index 4 is out of range. Furthermore, the code is not performing the action we want, which is to iterate over the list indices and print the elements at each index.

Solution

We can use the range() function to iterate through the list of numbers to solve this problem. The range() function returns a sequence of numbers starting from 0 by default, increments by one by default, then stops before a specified number.

Let’s look at the revised code:

numbers = [4, 32, 12, 7]

for number in range(len(numbers)):

    print(numbers[number])

Let’s run the code to get the output:

4
32
12
7

The code successfully prints all the values from the numbers array to the console. We can also use the in operator without using indexing, for example:

numbers = [4, 32, 12, 7]

for number in numbers:

    print(number)
4
32
12
7

Summary

Congratulations on reading to the end of this tutorial! The error “IndexError: list index out of range” occurs when you attempt to access a list with an index value that is outside the range of possible index values. If you use a loop to iterate over a list and encounter this error, use print statements to pinpoint the index values you are selecting. Ensure that the loop accounts for list indexing starting from zero.

You can use the range() function on the length of the list to get the list of appropriate index values to iterate over, or you can use a for… in loop and not use indexing at all.

For further reading on Python IndexError, go to the articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.