How to Check If an Array is Empty in Python

09/12/2021

Contents

In this article, you will learn how to check if an array is empty in Python.

Check If an Array is Empty

There are several ways to check if an array, or any data structure, is empty in Python. Here are some common methods:

Using the len() function

The built-in len() function can be used to check the length of an array. If the length is 0, the array is considered empty.

array = []
if len(array) == 0:
    print("The array is empty")

Using an if statement

You can use an if statement to check if an array is empty by checking if its length is equal to 0.

array = []
if not array:
    print("The array is empty")

Using a boolean test

Another way to check if an array is empty is to use a boolean test to see if the array is truthy or falsy. In Python, an empty array is considered falsy.

array = []
if not array:
    print("The array is empty")

These methods can be used to check if any data structure, such as a list, tuple, or dictionary, is empty in Python.