What does the Python error “List index out of range” mean?

0

So you were working with a list or an array in Python and probably tried to slice it. But instead of the expected result you get an error that says “list index out of range”. No worries, it can happen to anyone.

Let’s explore what this error means, its cause, and how to remove it without further ado.

What does the “list index out of range” error mean in Python?

When Python throws a “list index out of range” error, it means you tried to slice the list beyond its last index.

Python identifies each item in a list by its index. And the index of a list starts at zero. For example, when you have a list of five elements, its first element is on index zero, while the last is on index four.

For example, in a list of five programming languages:

Languages = ["Python", "Go", "PHP", "C", "Perl"]

The indexing of the above list is between zero and four. So trying to slice it to print the fifth element as shown below gives an error:

print(languages[5])
Output:
IndexError: list index out of range

In the example above, Python is trying to find the fifth index in the list, and when it can’t find it, it throws the list index error. This is because the first element (Python) is at index zero, while the last (Perl) is at index four.

This is the basis of the “list index out of range” error. As said, Python throws it whenever you try to slice a list beyond its last index.

USE VIDEO OF THE DAY

How to remove “list index out of range” error in Python

So how can you remove this error? It’s easy.

Following the previous section above, you can print the indexes using a for loop in a list comprehension:

indexes = [languages.index(i) for i in languages]
print(indexes)
Output:
[0, 1, 2, 3, 4]

The index of a list is the basis of slicing in programming. So, since you know the highest index in the list for the output above (4), you can decipher the slice boundary.

Therefore, to slice the list and get the last element:

print(languages[4])
Output:
Perl

It now shows the correct result.

What if you want to browse the list using its index?

Besides the classic Python for loop, you can also use the concept of index to iterate through a list. Although this method may seem daunting, sometimes it is unavoidable. For example, this comes in handy if you want to sort a list in reverse order.

This method works by setting an initial index and incrementing or decrementing it by one to the last available index.

To print the articles with an increasing index number (from the first to the last article), for example:

index = 0 # Initial index 
for i in Languages:
print(Languages[index])
index +=1
Output:
Python
Go
PHP
C
Perl

But what if you set the initial index to one instead of zero? Looked:

index = 1 # Initial index 
for i in Languages:
print(Languages[index])
index +=1
Output:
Go
PHP
C
Perl
IndexError: list index out of range

In the example above, indexing starts from the second element (index one, Goes). So when incrementing, the loop doesn’t stop until it has finished counting the five items. This forces the index to increase by one until the last element.

Therefore, the slice reaches a fifth index which is not available. So it throws index error. This means that the index increases according to the following pattern for each element:

1=1, 1+1=2, 1+2=3, 1+3=4, 1+4=5 

Instead of the correct pattern, which is:

0=0, 0+1=1, 1+1=2, 1+2=3, 1+3=4

As you can see, the highest index value above is four, which is correct since the loop starts incrementing indexing from zero.

Therefore, setting the initial index to zero as you did in the first example in this section removes the “list index out of range” error:

index = 0 # Initial index 
for i in Languages:
print(Languages[index])
index +=1 #Increase the index by one for each iteration

To apply this concept to outputting the elements in reverse order, you would need to subtract one from the length of the array. So this forces the index to start from four and count down to the first index, zero.

This is useful if you are unsure of the length value of the list coming from a source, such as a database.

Here is an example :

index = (len(Languages)-1)
for i in Languages:
print(Languages[index])
index -=1 #Decrease the index by one for each iteration
Output:
Perl
C
PHP
Go
Python

But not subtracting one from the length of the list generates the “list out of range index” error:

index = (len(Languages)-1)
for i in Languages:
print(Languages[index])
index -=1
Output:
IndexError: list index out of range

The code above throws an index error because the length of the list is five, and it tries to start indexing from five to zero, when the highest index is four. So this means that the list has no fifth index (sixth element).

Get creative management lists in Python

Python errors are friendly and generally readable. Invariably, this makes them traceable to a reasonable extent.

As you have learned, it is quite easy to remove the list index out of range error. If you encounter this error in your future programs, regardless of the complexity of the list, you can apply the concepts explained in this article to solve the problem.

Feel free to get creative with complex lists. You might also want to learn how to sort lists in Python in your spare time to understand all of this better.


Computer screen with blocks of code
How to Sort Almost Any Type of List in Python

Lists with multiple data types can quickly get out of hand. Here’s how to sort them with Python.

Read more


About the Author

Share.

About Author

Comments are closed.