How to Check if an Array is Empty in Python?

In this tutorial, I will explain how to check if an array is empty in Python. One of my team members asked this question, and I decided to add this to my article, I will help you understand the different methods available and how to use them effectively with examples.

Check if a Python Array is Empty

There are several ways to check if an array (or list) is empty in Python. Here, we will discuss the most common and efficient methods.

Read How to Sort an Array in Python

1. Use the not Operator

The not operator is one of the simplest ways to check if a list is empty. In Python, an empty list is considered as False, while a non-empty list is True. Therefore, you can use the not operator to check for an empty list.

Example:

shopping_cart = []

if not shopping_cart:
    print("The shopping cart is empty.")
else:
    print("The shopping cart has items.")

Output:

The shopping cart is empty.

I have executed the above example code and added the screenshot below.

Check if an Array is Empty in Python

In this example, the shopping_cart list is empty, so the condition not shopping_cart evaluates to True, and the message “The shopping cart is empty.” is printed.

Check out How to Use Python Array Index -1.

2. Use the len() Function

The len() function returns the number of items in a list. If the list is empty, len() returns 0. You can use this to check if a list is empty.

Example:

shopping_cart = ["Apple", "Banana"]

if len(shopping_cart) == 0:
    print("The shopping cart is empty.")
else:
    print("The shopping cart has items.")

Output:

The shopping cart has items.

I have executed the above example code and added the screenshot below.

How to Check if an Array is Empty in Python

Here, the shopping_cart list contains items, so len(shopping_cart) returns a value greater than 0, and the message “The shopping cart has items.” is printed.

Read How to Save an Array to a File in Python

3. Comparing with an Empty Python List

Another simple method is to compare the list directly with an empty list. [].

Example:

shopping_cart = []

if shopping_cart == []:
    print("The shopping cart is empty.")
else:
    print("The shopping cart has items.")

In this example, the shopping_cart list is empty, so the condition shopping_cart == [] evaluates to True, and the message “The shopping cart is empty.” is printed.

Check out How to Find the Number of Elements in a Python Array

4. Use the bool() Function

The bool() function converts a value to a Boolean. An empty list converts to False, while a non-empty list converts to True.

Example:

shopping_cart = ["Laptop", "Mouse"]

if not bool(shopping_cart):
    print("The shopping cart is empty.")
else:
    print("The shopping cart has items.")

Output:

The shopping cart has items.

I have executed the above example code and added the screenshot below.

Check if an Array is Empty in Python Use the bool() Function

Here, the shopping_cart list contains items, so bool(shopping_cart) returns True, and the message “The shopping cart has items.” is printed.

Read How to Transpose an Array in Python

5. Use NumPy Arrays in Python

In some cases, you might be working with NumPy arrays instead of lists. Checking if a NumPy array is empty requires a slightly different approach. You can use the size the attribute of a NumPy array to determine if it is empty.

Example:

import numpy as np

data = np.array([])

if data.size == 0:
    print("The NumPy array is empty.")
else:
    print("The NumPy array has elements.")

Output:

The NumPy array is empty.

In this example, the data array is empty, so data.size returns 0, and the message “The NumPy array is empty.” is printed.

Check out How to Remove the First Element from an Array in Python

Use Cases

Let’s consider a real-world scenario where you are developing a to-do list application for residents in San Francisco. You need to check if the to-do list is empty before displaying a message to the user.

Example:

todo_list = []

if not todo_list:
    print("Your to-do list is empty. Add some tasks to get started!")
else:
    print("You have tasks in your to-do list.")

Output:

Your to-do list is empty. Add some tasks to get started!

In this example, if the todo_list is empty, the user is prompted to add tasks.

Check out How to Reshape an Array in Python Using the NumPy Library

Handling Edge Cases

When checking if an array is empty, it’s important to consider edge cases. For instance, a list might contain None values or other false values. Ensure that your checks are robust enough to handle such cases.

Example:

shopping_cart = [None, None]

if not shopping_cart:
    print("The shopping cart is empty.")
else:
    print("The shopping cart has items.")

Output:

The shopping cart has items.

In this example, the shopping_cart list is not empty, even though it contains None values. The condition not shopping_cart evaluates False, and the message “The shopping cart has items.” is printed.

Read How to Count Occurrences in Python Arrays

Conclusion

In this tutorial, I explained various methods to check if an array is empty in Python. Whether you are using the not operator, the len() function, comparing with an empty list, or using the bool() function, each method has its advantages. I covered how to check if a NumPy array is empty. We also discussed real-time use cases, and saw how to handle edge cases.

You may also 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.