In python programming, we often need to check a numpy ndarray is empty or not. In this tutorial, we will introduce numpy beginners how to do.
Create an empty ndarray in numpy
It is very easy to create an empty array in numpy, you can create as follow:
import numpy as np ys = np.array(, dtype=np.int64)
In this code, ys is an empty numpy array.
How to check a numpy array is empty or not
We can use ndarray.size to check
ndarray.size illustrates the count of elements in a numpy array. If the value of it is 0, which means this numpy array is empty. Here is an example:
if ys.size > 0: print("ys array is not empty") else: print("ys array is empty")
Run this code, you will get this result:
ys array is empty