An illustration of Python logo along with its applications

What Does the Python “List index out of range” Error Mean?

So that you have been working with a listing or array in Python and doubtless tried to slice it. However as an alternative of the anticipated outcome, you get an error that claims, “listing index out of vary.” No worries, it may occur to anybody.

Let’s discover what this error means, its trigger, and methods to take away it with none additional ado.

What Does the “listing index out of vary” Error Imply in Python?

When Python throws a “listing index out of vary” error, it means you tried to slice the listing past its final index.

Python identifies every merchandise in a listing by its index. And the index of a listing begins from zero. As an example, when you could have a listing with 5 objects, its first merchandise is on index zero, whereas the final is on the fourth index.

For instance, in a listing of 5 programming languages:

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

The indexing for the above listing is between zero and 4. So attempting to slice it to print the fifth merchandise as demonstrated beneath offers an error:

print(languages[5])
Output:
IndexError: listing index out of vary

Within the above instance, Python tries to search for the fifth index within the listing, and when it may’t discover it, it throws the listing index error. That is as a result of the primary factor (Python) is on index zero, whereas the final (Perl) is on index 4.

That is the premise of the “listing index out of vary” error. As mentioned, Python throws it everytime you attempt to slice a listing past its final index.

MAKEUSEOF VIDEO OF THE DAY

The way to Take away the “listing index out of vary” Error in Python

So how are you going to take away this error? It is simple.

Additional to the earlier part above, you’ll be able to print the indexes utilizing a for loop in a listing comprehension:

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

The index of a listing is the premise of slicing in programming. So since you recognize the very best index of the listing for the output above (4), you’ll be able to decipher the slice restrict.

Therefore, to slice the listing and get the final merchandise:

print(languages[4])
Output:
Perl

It now outputs the right outcome.

What if You Wish to Loop By way of the Checklist Utilizing Its Index?

In addition to the common Python for loop, you can too use the index idea to iterate by way of a listing. Whereas this methodology might look arduous, generally it is unavoidable. As an example, it is useful if you wish to kind a listing in reverse order.

This methodology works by setting an preliminary index and incrementing or decrementing it by one till the final out there index.

To print the objects with an rising index quantity (the primary to the final merchandise), for instance:

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

However what occurs when you set the preliminary index to at least one as an alternative of zero? Take a look:

index = 1 # Preliminary index 
for i in Languages:
print(Languages[index])
index +=1
Output:
Go
PHP
C
Perl
IndexError: listing index out of vary

Within the above instance, the indexing begins from the second merchandise (index one, Go). So whereas incrementing, the loop would not cease till it completes the rely for the 5 objects. This forces the index to extend by one till the final merchandise.

Therefore, the slice hits a fifth index which is not out there. So it throws an index error. It means the index will increase within the following sample for every merchandise:

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

As an alternative of the right sample, which is:

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

As you’ll be able to see, the very best worth of the above index is 4, which is right for the reason that loop begins to increment the indexing from zero.

Due to this fact, setting the preliminary index to zero as you probably did within the first instance on this part removes the “listing index out of vary” error:

index = 0 # Preliminary index 
for i in Languages:
print(Languages[index])
index +=1 #Improve the index by one for every iteration

To use this idea for outputting the objects in reverse order, you may must subtract one from the array’s size. So this forces the index to begin from 4 and rely right down to the primary index, zero.

That is useful when you’re unsure concerning the size worth of the listing coming from a supply, say, a database.

Here is an instance:

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

However failing to subtract one from the size of the listing throws the “listing out of vary index” error:

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

The above code throws an index error as a result of the size of the listing is 5, and it tries to begin indexing down from 5 to zero, whereas the very best index is 4. So it means the listing would not have a fifth index (sixth merchandise).

Get Artistic Dealing with Lists in Python

Python’s errors are human-friendly and usually readable. Invariably, this makes them traceable to an inexpensive extent.

As you’ve got realized, eradicating the listing index out of vary error is fairly simple. If you happen to face this error in your future packages, no matter how complicated the listing is, you’ll be able to apply the ideas defined on this submit to resolve the issue.

Be happy to get artistic with complicated lists. You may additionally wish to discover ways to kind lists in Python in your free time to get a greater hold of all of it.


Computer screen with blocks of code
The way to Kind Nearly Any Kind of Checklist in Python

Lists with a number of knowledge varieties can get out of hand shortly. Here is methods to kind them with Python.

Learn Subsequent


About The Creator

Leave a Comment

Your email address will not be published. Required fields are marked *