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

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

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



So you had been working with a listing or array in Python and possibly tried to slice it. But as a substitute of the anticipated end result, you get an error that claims, “list index out of range.” No worries, it could possibly occur to anybody.

Let’s discover what this error means, its trigger, and how you can take away it with none additional 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 checklist past its final index.

Python identifies every merchandise in a listing by its index. And the index of a listing begins from zero. For occasion, when you’ve gotten 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 checklist is between zero and 4. So attempting to slice it to print the fifth merchandise as demonstrated under provides an error:

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

In the above instance, Python tries to search for the fifth index in the checklist, and when it could possibly’t discover it, it throws the checklist index error. That’s as a result of the first ingredient (Python) is on index zero, whereas the final (Perl) is on index 4.

That’s the foundation of the “list index out of range” error. As mentioned, Python throws it everytime you attempt to slice a listing past its final index.

MAKEUSEOF VIDEO OF THE DAY

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

So how will you take away this error? It’s simple.

Further to the earlier part above, you may 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 foundation of slicing in programming. So since you realize the highest index of the checklist for the output above (4), you may decipher the slice restrict.

Hence, to slice the checklist and get the final merchandise:

print(languages[4])
Output:
Perl

It now outputs the appropriate end result.

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

Besides the common Python for loop, you may also use the index idea to iterate by a listing. While this methodology could look arduous, generally it is unavoidable. For occasion, it turns out to be 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 obtainable index.

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

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

But what occurs if you happen to set the preliminary index to at least one as a substitute of zero? Have a glance:

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

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

Hence, the slice hits a fifth index which is not obtainable. So it throws an index error. It means the index will increase in the following sample for every merchandise:

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

Instead of the appropriate sample, which is:

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

As you may see, the highest worth of the above index is 4, which is appropriate since the loop begins to increment the indexing from zero.

Therefore, setting the preliminary index to zero as you probably did in the first instance on this part 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 every iteration

To apply this idea for outputting the objects in reverse order, you will have to subtract one from the array’s size. So this forces the index to begin from 4 and depend right down to the first index, zero.

This is useful if you happen to’re unsure about the size worth of the checklist coming from a supply, say, a database.

Here’s an instance:

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

But failing to subtract one from the size of the checklist throws the “list out of range index” error:

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

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

Get Creative Handling Lists in Python

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

As you have realized, eradicating the checklist index out of vary error is fairly simple. If you face this error in your future applications, regardless of how complicated the checklist is, you may apply the ideas defined on this publish to resolve the downside.

Feel free to get artistic with complicated lists. You may additionally wish to learn to kind lists in Python in your free time to get a greater grasp of all of it.


About The Author



Source link

Exit mobile version