How to check whether a NumPy array is empty or not?

Check Empty NumPy Array: In this tutorial, we will learn how to check whether a NumPy array is empty or not using different methods? By Pranit Sharma Last updated : May 26, 2023

Problem Statement

Given a NumPy array, we have to check whether it is an empty array or not.

Checking whether a NumPy array is empty or not

To check an empty NumPy array, there are multiple methods that you can use such as numpy.ndarray.size attribute, numpy.any() method, and numpy.size() method. Let's discuss all these methods with examples.

1) Using numpy.ndarray.size Attribute

The ndarray.size attribute returns the total number of elements in a NumPy array. If the given array is empty, it returns 0; the Number of elements, otherwise.

Syntax

ndarray.size

Example 1:

Consider the below Python program to check NumPy array is empty or not using ndarray.size attribute.

# Import NumPy
import numpy as np

# Creating empty and non-empty arrays
arr1 = np.array([])
arr2 = np.array([10, 20, 30])

# Printing original arrays
print("arr1:\n", arr1, "\n")
print("arr2:\n", arr2, "\n")

# Check whether array is empty or not
# using ndarray.size attribute
if arr1.size == 0:
    print("arr1 is an empty array")
else:
    print("arr1 is not an empty array")

if arr2.size == 0:
    print("arr2 is an empty array")
else:
    print("arr2 is not an empty array")

Output

arr1:
 [] 

arr2:
 [10 20 30] 

arr1 is an empty array
arr2 is not an empty array

2) Using numpy.any() Method

The numpy.any() method checks whether any array element along a given axis evaluates to True. If the given array is not empty it returns True; False, otherwise.

Syntax

numpy.any(a, axis=None, out=None, keepdims=<no value>, *, where=<no value>)

Example 2

Consider the below Python program to check NumPy array is empty or not using numpy.any() method.

# Import NumPy
import numpy as np

# Creating empty and non-empty arrays
arr1 = np.array([])
arr2 = np.array([10, 20, 30])

# Printing original arrays
print("arr1:\n", arr1, "\n")
print("arr2:\n", arr2, "\n")

# Check whether array is empty or not
# using numpy.any() method
if np.any(arr1) == False:
    print("arr1 is an empty array")
else:
    print("arr1 is not an empty array")

if np.any(arr2) == False:
    print("arr2 is an empty array")
else:
    print("arr2 is not an empty array")

Output

arr1:
 [] 

arr2:
 [10 20 30] 

arr1 is an empty array
arr2 is not an empty array

3) Using numpy.size() Method

The numpy.size() method returns the total number of elements in an array along a given axis (optional). If the given array is empty, it returns 0; the Number of elements, otherwise.

Syntax

numpy.size(arr, axis=None)

Example 3

Consider the below Python program to check NumPy array is empty or not using numpy.size() method.

# Import NumPy
import numpy as np

# Creating empty and non-empty arrays
arr1 = np.array([])
arr2 = np.array([10, 20, 30])

# Printing original arrays
print("arr1:\n", arr1, "\n")
print("arr2:\n", arr2, "\n")

# Check whether array is empty or not
# using numpy.size() method
if np.size(arr1) == 0:
    print("arr1 is an empty array")
else:
    print("arr1 is not an empty array")

if np.size(arr2) == 0:
    print("arr2 is an empty array")
else:
    print("arr2 is not an empty array")

Output

arr1:
 [] 

arr2:
 [10 20 30] 

arr1 is an empty array
arr2 is not an empty array

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.