How to Check if an Array Index Exists in Python?

You’re looping through a list of monthly sales, and suddenly your script crashes with IndexError: list index out of range. You were sure that index 5 existed, but your data source had fewer months than you expected. Now your report is broken, and you’re not sure why.

This is a very common problem when you work with lists, arrays, or any kind of indexed data in a Python script. Maybe you’re reading rows from a CSV file, pulling JSON from an API, or cleaning a messy dataset. Anytime you access an index that doesn’t exist, Python raises an error, and your program stops.

The good news is that there are simple, reliable ways to check if an array index exists in Python before you use it, so your code stays safe and easy to debug. In this article, I’ll show you 4 ways to do how to check if an array index exists in Python.

We’ll use the same dataset in all methods: a list of monthly sales in dollars.

# Monthly sales data for one product (in dollars)
monthly_sales = [1200, 1350, 1600, 1450, 1700, 1550]

Think of monthly_sales[0] as January, monthly_sales[1] as February, and so on.

Method 1 – Use len() to Guard the Index

Use this method if you want a simple, pure Python check with no extra libraries. It’s perfect when you know the index you want and you just want to confirm it exists.

Step 1: Define your data and index

import sys  # Not required, but included to show standard imports

monthly_sales = [1200, 1350, 1600, 1450, 1700, 1550]

index_to_check = 4 # For example, the 5th month

Step 2: Check index with len() before accessing

import sys

monthly_sales = [1200, 1350, 1600, 1450, 1700, 1550]
index_to_check = 4 # 0-based index

if 0 <= index_to_check < len(monthly_sales):
print(f"Index {index_to_check} exists. Sales:", monthly_sales[index_to_check])
else:
print(f"Index {index_to_check} does NOT exist. List length is {len(monthly_sales)}.")

Sample output:

Index 4 exists. Sales: 1700

You can see the output in the screenshot below.

Check Array Index Exists Python

How does this code work?
You use the built-in len() function to get the length of monthly_sales. Valid indices are from 0 up to len(monthly_sales) - 1. The condition 0 <= index_to_check < len(monthly_sales) makes sure the index is inside this range before you access monthly_sales[index_to_check]. If it’s outside, you print a helpful message instead of crashing your script.

Pro Tip: Use the len() check whenever you access a specific index from user input or external data. It’s fast and works in both Python lists and many other sequence types.

Method 2 – Use try / except IndexError

Use this method when the length of your data is unpredictable, such as reading from an API or a CSV file where some rows may be shorter than others.

Step 1: Define your data and index

import sys

monthly_sales = [1200, 1350, 1600, 1450, 1700, 1550]

index_to_check = 10 # Intentionally out of range

Step 2: Safely access the index with try / except

import sys

monthly_sales = [1200, 1350, 1600, 1450, 1700, 1550]
index_to_check = 10 # This index doesn't exist

try:
value = monthly_sales[index_to_check]
print(f"Index {index_to_check} exists. Sales:", value)
except IndexError:
print(f"Index {index_to_check} does NOT exist in monthly_sales.")

Sample output:

Index 10 does NOT exist in monthly_sales.

You can see the output in the screenshot below.

Check Array Index Exists in Python

How does this code work?
You try to access monthly_sales[index_to_check] inside a try block. If the index is valid, Python returns the value and prints it. If the index doesn’t exist, Python raises an IndexError. The except IndexError block catches that error and shows a friendly message instead of stopping your program.

Pro Tip: Use try / except IndexError when you process real-world data and you’re not sure how long each list will be. It keeps your script running even when the data is messy.

Method 3 – Use range(len(…)) to Validate Index

Use this method when you loop over indices or need to check multiple indices while iterating through your data. It keeps all indices inside the valid range.

Step 1: Define your data and a list of indices to check

import sys

monthly_sales = [1200, 1350, 1600, 1450, 1700, 1550]

indices_to_check = [0, 2, 5, 6] # Check multiple indices

Step 2: Use range(len(monthly_sales)) to test each index

import sys

monthly_sales = [1200, 1350, 1600, 1450, 1700, 1550]
indices_to_check = [0, 2, 5, 6]

valid_indices = range(len(monthly_sales))

for idx in indices_to_check:
if idx in valid_indices:
print(f"Index {idx} exists. Sales:", monthly_sales[idx])
else:
print(f"Index {idx} does NOT exist. Valid indices: 0 to {len(monthly_sales) - 1}.")

Sample output:

Index 0 exists. Sales: 1200
Index 2 exists. Sales: 1600
Index 5 exists. Sales: 1550
Index 6 does NOT exist. Valid indices: 0 to 5.

You can see the output in the screenshot below.

Check Python Array Index Exists

How does this code work?
The built-in range() function creates a sequence of numbers from 0 up to but not including len(monthly_sales). That sequence represents all valid indices in the list. You then loop over indices_to_check and use the in operator to see if each index is inside valid_indices before accessing monthly_sales[idx].

Pro Tip: Use range(len(my_list)) whenever you need to work with indices in a loop. It guarantees you never go past the end of the list and avoids IndexError in index-based loops.

Method 4 – Use NumPy Arrays with Safe Checks

Use this method when you already work with NumPy for data analysis, scientific computing, or performance-heavy tasks. NumPy arrays behave like lists for indexing but are optimized for numeric operations.

Step 1: Install and import NumPy, define your sales array

import numpy as np

monthly_sales = np.array([1200, 1350, 1600, 1450, 1700, 1550])

index_to_check = 3 # For example, the 4th month

Step 2: Combine len() with NumPy indexing

import numpy as np

monthly_sales = np.array([1200, 1350, 1600, 1450, 1700, 1550])
index_to_check = 3 # 0-based index

if 0 <= index_to_check < len(monthly_sales):
print(f"Index {index_to_check} exists. Sales:", monthly_sales[index_to_check])
else:
print(f"Index {index_to_check} does NOT exist in NumPy array.")

Sample output:

Index 3 exists. Sales: 1450

How does this code work?
A NumPy array uses the same zero-based indexing rules as a normal Python list. You still use len() to get the number of elements and check whether the index is between 0 and len(monthly_sales) - 1. Once the index passes that test, you safely access monthly_sales[index_to_check] just like a list element.b

Pro Tip: If your project already uses NumPy for numeric work, stick to NumPy arrays and reuse the same index-checking pattern with len(). It keeps your code consistent and easy to maintain.

Things to Keep in Mind

  • Lists are zero-indexed. The first element is at index 0, not 1, so a list of length 6 has valid indices 0 to 5.
  • Negative indices are valid. Index -1 gives you the last element, -2 the second last, so don’t treat negative indices as “does not exist” unless you add that rule yourself.
  • Don’t confuse value vs. index. Checking 3 in monthly_sales tests if the value 3 exists, not if index 3 exists. Always use len() or range(len(…)) for index checks.
  • Handle empty lists carefully. If monthly_sales is empty, len(monthly_sales) is 0, and any non-negative index is invalid. Check if monthly_sales or if len(monthly_sales) > 0 before accessing.
  • Catching errors is not enough. try / except IndexError keeps your script alive, but you should still log or handle why the index was wrong, especially in production code.
  • Version differences mostly affect print and division. Index checking with len() and range() works the same in Python 3 and Python 2, but always test your script in the Python version you deploy.

You’ve seen four practical ways to check if an array index exists in Python: using len(), using try / except IndexError, using range(len(…)) with the in operator, and using the same checks on NumPy arrays. Use the len() approach for simple index checks, try / except for unpredictable external data, and NumPy when you work in data-heavy or scientific projects. I hope you found this article helpful.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.