Skip to content
Advertisement

How can I check whether a numpy array is empty or not?

How can I check whether a numpy array is empty or not?

I used the following code, but this fails if the array contains a zero.

if not self.Definition.all():

Is this the solution?

if self.Definition == array([]):

Advertisement

Answer

You can always take a look at the .size attribute. It is defined as an integer, and is zero (0) when there are no elements in the array:

import numpy as np
a = np.array([])

if a.size == 0:
    # Do something when `a` is empty
Advertisement