Check if NumPy Array is Empty in Python [4 Methods]

Are you searching for ways to check if NumPy array is empty in Python? Here, in this Python NumPy article, I will explain what are the different ways present in Python to check if the NumPy array is empty.

To check if NumPy array is empty in Python, we can use some functions like the size() function will provide the number of elements in the array, any() provides boolean values if any True value is present inside the array, shape() provides the dimension of the array, and tolist() will convert the array into a list and then the len() will provide the length of the list.

Check if NumPy array is empty in Python methods

There are four different methods present to check if the NumPy array is empty in Python:

  1. size() function
  2. shape() function
  3. any() function
  4. tolist() with len() function

We will see each of these methods one by one with some illustrative examples:

Note: We will use conditional statements like if and else in each case to check if the NumPy array is empty in Python.

Method 1: NumPy check empty array in Python using size() function

The size() method in the NumPy Python library returns the number of elements in the array. We can use this method to check if a NumPy array is empty by comparing its size to zero.

Example: Let’s create an array in Python using the NumPy library and then check if that is empty in Python using the size() function.

import numpy as np

temperatures = np.array([68, 72, 73, 75, 79, 82, 81])
if temperatures.size == 0:
    print("The NumPy array is empty.")
else:
    print("The NumPy array is not empty.")

Output: The implementation of the code with a screenshot is mentioned below.

The NumPy array is not empty.
numpy isempty in Python

This way we can use the shape() function to check if NumPy array is empty in Python.

Method 2: NumPy array empty check in Python using shape() function

The shape() method in the NumPy Python library returns a tuple in Python representing the dimensions of the array. An empty array will have a shape of (0,). We can use this method to check if a NumPy array is empty in Python.

Example: Here, Let’s create an empty numpy array in Python in the initial stage where after some time we will store our data inside it through Python. So, during the time of storing data, we first need to check if the NumPy array is empty in Python.

import numpy as np

population_data = np.array([])
if population_data.shape == (0,):
    print("The population data array is empty.")
else:
    print("The population data array is not empty.")

Output: The implementation of the above Python code:

The population data array is empty.
numpy check if array is empty in Python

To check if NumPy array is empty in Python using the shape() function.

Method 3: Check if a NumPy array is empty in Python using any() function

The any() method in the NumPy Python library can be used to check if any element in the array evaluates to True. An empty array will return False when passed to NumPy any() function, as there are no elements to evaluate.

Example: Consider a situation where we have to check if the NumPy array is empty in Python using any() function.

import numpy as np

gdp_growth_rates = np.array([])
if gdp_growth_rates.any():
    print("There are positive GDP growth rates in the array.")
else:
    print("There are no positive GDP growth rates in the array.")

Output: The implementation of the code with a screenshot.

There are no positive GDP growth rates in the array.
np.isempty Python

This way we can use the any() function to check if the NumPy array is empty in Python.

Method 4: if NumPy array is empty in Python using the tolist() with len() function

Another way to check if a NumPy array is empty in Python is by converting it to a Python list using the tolist() method and then finding the length of the Python list using the len() function.

Example: For instance, let’s take an array and check if it is empty or not through a Python function.

import numpy as np

national_parks_array = np.array([])
national_parks_list = national_parks_array.tolist()

if len(national_parks_list) == 0:
    print("The list of national parks is empty.")
else:
    print("The list of national parks is not empty.")

Output: The implementation of the Python code is:

The list of national parks is empty.
check if numpy array is empty in Python

To check if the NumPy array is empty in Python we can convert the array into a Python list using tolist() and then check if the length of the list is zero using the len() function.

Conclusion

Understanding all the methods to check if NumPy array is empty in Python such as the size() function, shape() function, any() function, and tolist() function with len() function with some illustrative examples. Depending on our specific use case and coding style, we can choose the method that best suits our needs.

You may also like to read: