In this tutorial, we will look at how to check if a given Numpy array is empty or not with the help of some examples.

## Steps to check whether an array is empty

An array is said to be empty if it does not contain any value. There are a number of ways to check for an empty array in Numpy –

- Check whether the array’s length is 0.
- Use the array’s
`.size`

attribute and check if it’s equal to 0 (an array’s size is zero if it has no elements). - Use the array’s
`.shape`

attribute which returns the shape of a numpy array. If the array is empty, the first value in the returned tuple would be 0.

The following is the syntax –

## check if numpy array is empty # using the len() function len(ar) == 0 # using the .shape attribute ar.size == 0 # using the .shape attribute ar.shape[0] == 0

Let’s now look at some examples of using the above syntax –

Let’s create some Numpy arrays that we will be using throughout this tutorial.

import numpy as np # an empty array ar1 = np.array([]) # a non-empty array ar2 = np.array([1, 2, 3]) # array with None values ar3 = np.array([None, None])

### Example 1 – Check if a Numpy array is empty using the `len()`

function

The idea here is that, for an empty array, the length will be 0. So we can check whether an array is empty or not by comparing its length with 0.

Let’s now check whether the three arrays created above are empty or not.

# check if array is empty print(len(ar1)==0) print(len(ar2)==0) print(len(ar3)==0)

Output:

True False False

We get `True`

for `ar1`

which is an empty array and `False`

for `ar2`

and `ar3`

which are non-empty arrays. Note that an array with `None`

values is considered non-empty.

### Example 2 – Check if a Numpy array is empty using the `.size`

property

The `.size`

property of a Numpy array returns the number of elements in an array. For an empty array, its size should be zero.

# check if array is empty print(ar1.size == 0) print(ar2.size == 0) print(ar3.size == 0)

Output:

True False False

We get the same results as above. `True`

for `ar1`

which is empty and `False`

for the other arrays (which are not empty).

### Example 3 – Check if a Numpy array is empty using the `.shape`

property

The `.shape`

property of an array gives the shape of the array. For example, for a 2D array, it gives the number of rows and the number of columns. To check if an array is empty, we may use the shape to determine whether the first dimension is 0 or not.

# check if array is empty print(ar1.shape[0] == 0) print(ar2.shape[0] == 0) print(ar3.shape[0] == 0)

Output:

True False False

We get the same result as above.

## Methods to avoid

There can be other methods as well but be careful about what these methods are actually checking for. For example, you can use the `numpy.any()`

function on an empty array and it will return `False`

, on the other hand, it’ll also return `False`

for a numpy array with `False`

values (which is not considered an empty array).

Let’s see this in action.

# create numpy arrays ar1 = np.array([]) ar2 = np.array([False, False, False]) # check if the above arrays are empty print(np.any(ar1)) print(np.any(ar2))

Output:

False False

We get `False`

for both, thus, this method cannot reliably be used to check whether a numpy array is empty or not.

You might also be interested in –

- Numpy – Check If an Array contains a NaN value
- Check If Two Numpy Arrays are Equal
- Numpy – Check If an Element is NaN

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**