So you were working with a list or 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 any 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 from zero. For instance, when you have a list with five items, its first item is on index zero, while the last is on the fourth index.

For example, in a list of five programming languages:

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

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

        print(languages[5])

<strong>Output</strong>:

IndexError: list index out of range

In the above example, Python tries to look for the fifth index in the list, and when it can't find it, it throws the list index error. That's because the first element (Python) is on index zero, while the last (Perl) is on index four.

That's 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.

How to Remove the "list index out of range" Error in Python

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

Further to 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)

<strong>Output</strong>:<strong>
</strong>[0, 1, 2, 3, 4]

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

Hence, to slice the list and get the last item:

        print(languages[4])
<strong>Output</strong>:<strong>
</strong>Perl

It now outputs the correct result.

What if You Want to Loop Through the List Using Its Index?

Besides the regular Python for loop, you can also use the index concept to iterate through a list. While this method may look arduous, sometimes it's unavoidable. For instance, it 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 until the last available index.

To print the items with an increasing index number (the first to the last item), for example:

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

But what happens if you set the initial index to one instead of zero? Have a look:

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

In the above example, the indexing starts from the second item (index one, Go). So while incrementing, the loop doesn't stop until it completes the count for the five items. This forces the index to increase by one until the last item.

Hence, the slice hits a fifth index which isn't available. So it throws an index error. It means the index increases in the following pattern for each item:

        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 value of the above index is four, which is correct since the loop starts to increment the 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  <strong># Initial index</strong> 
for i in Languages:
    print(Languages[index])
    index +=1 <strong>#Increase the index by one for each iteration
</strong>

To apply this concept for outputting the items in reverse order, you'll need to subtract one from the array's length. So this forces the index to start from four and count down to the first index, zero.

This is helpful if you're not sure about the length value of the list coming from a source, say, a database.

Here's an example:

        index = (len(Languages)-1)
for i in Languages:
    print(Languages[index])
    index -=1 <strong>#Decrease the index by one for each iteration</strong>

<strong>Output</strong>:
Perl
C
PHP
Go
Python

But failing to subtract one from the length of the list throws the "list out of range index" error:

        index = (len(Languages)-1)
for i in Languages:
    print(Languages[index])
    index -=1

<strong>Output</strong>:
IndexError: list index out of range

The above code throws an index error because the length of the list is five, and it tries to start indexing down from five to zero, whereas the highest index is four. So it means the list doesn't have a fifth index (sixth item).

Get Creative Handling Lists in Python

Python's errors are human-friendly and typically readable. Invariably, this makes them traceable to a reasonable extent.

As you've learned, removing the list index out of range error is pretty easy. If you face this error in your future programs, regardless of how complex the list is, you can apply the concepts explained in this post 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 free time to get a better hang of it all.