Check if List Index Exists in Python (2 Examples)

 

Hi there! In this tutorial, I will show you 2 ways of checking if a list index exists in Python.

First, here is a quick overview of this tutorial:

Let’s dive into it!

 

Create Python List

Let us create a simple Python list of animals.

animals = ["dog", "goat", "cow", "sheep", "rabbit"]

 

Example 1: Use Python’s len() Function

In this example, we will use the Python len() function to check if the list index exists.

index = 4
print(index < len(animals))
 
# True

What we have done is to check if the index number is less than the length of the list.

If yes, then it will print out the boolean value True, which indicates that the index does exist in the list. But if the index is greater than the length of the list, then it will print out False, which means that the index does not exist in the list.
 

Example 2: Use “try” and “except” Blocks

In this second example, we are going to use the Python try and except blocks. If the index exists in the list, no error will be raised; but if the index does not exist in the list, then an index error will be raised.

animals = ["dog", "goat", "cow", "sheep", "rabbit"]
index = 4
try:
  animals[index]
  print("Index exists")
except IndexError:
  print("Index doesn't exist")
 
# Index exists

Let’s now adjust the index to 5, and see the output of the program.

animals = ["dog", "goat", "cow", "sheep", "rabbit"]
index = 5
try:
  animals[index]
  print("Index exists")
except IndexError:
  print("Index doesn't exist")
 
# Index doesn't exist

This raises an error because index number 5 is greater than the index of the last element in the list. Remember, Python indexing starts at 0!

That is how to check if a list index exists in Python. Hope you found this helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to check if a list index exists in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to check if a list index exists in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to check if a list index exists in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top