• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python Check if List Index Exists Using Python len() Function

Python Check if List Index Exists Using Python len() Function

February 10, 2022 Leave a Comment

To check if a list index exists in a list using Python, the easiest way is to use the Python len() function.

def indexExists(list,index):
    if 0 <= index < len(list):
        return True
    else:
        return False

print(indexExists([0,1,2,3,4,5],3))
print(indexExists(["This","is","a","list"],10))

#Output:
True
False

You can also check if an index exists using exception handling.

def indexExists(list,index):
    try:
        list[index]
        return True
    except IndexError:
        return False

print(indexExists([0,1,2,3,4,5],3))
print(indexExists(["This","is","a","list"],10))

#Output:
True
False

When working with collections, the worst feeling to experience is when we get an IndexError exception because we tried to access an element that doesn’t exist.

In Python, we can easily check if a list has an index so we won’t have to experience getting an IndexError.

To check if a certain list index exists, we check to see if the list index is between 0 and the length of the list.

Below is an example of a Python function which will return True or False depending on if the list index you want exists.

def indexExists(list,index):
    if 0 <= index < len(list):
        return True
    else:
        return False

print(indexExists([0,1,2,3,4,5],3))
print(indexExists(["This","is","a","list"],10))

#Output:
True
False

Checking if List Index Exists Using Exception Handling in Python

You can also check if an index exists using exception handling. When we try to access an element of a list with an index that is out of bounds, we will generate an IndexError.

Therefore, to check if a list index exists or not, we can see if our code raises an error when we try to access an element at a certain index.

Below is a Python function which uses exception handling to see if an index exists in a list.

def indexExists(list,index):
    try:
        list[index]
        return True
    except IndexError:
        return False

print(indexExists([0,1,2,3,4,5],3))
print(indexExists(["This","is","a","list"],10))

#Output:
True
False

Hopefully this article has been useful for you to understand how to check if a list index exists in Python.

Other Articles You'll Also Like:

  • 1.  Convert String to List Using Python
  • 2.  Python Random Boolean – How to Generate Random Boolean Values
  • 3.  How to Check If Value is in List Using Python
  • 4.  Convert List to Set with Python
  • 5.  Get Month Name from Datetime in pandas DataFrame
  • 6.  Python Check if Object Has Attribute
  • 7.  Get Elapsed Time in Seconds in Python
  • 8.  pandas mean – Get Average of Series or DataFrame Columns
  • 9.  How to Cube Numbers in Python
  • 10.  Using Python Selenium Webdriver to Refresh Page

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy