Hello coders!! In this article, we will be learning different ways to check if the NumPy array is empty or not in Python. **An array is a collection of elements of the same data type in a contiguous memory location. We use NumPy in python to perform array related manipulations.**

Contents

## 5 Methods to Find out if Numpy Array is Empty:

The 5 methods which we will cover in this post are following:

- numpy.any() to check if the NumPy array is empty in Python
- numpy.size() method
- By Converting it to list
- Using arr.size()
- By using arr.shape() method

Let’s look at every method in detail with the help of examples.

## Method 1: numpy.any() to check if the NumPy array is empty in Python

numpy.any() method is used to test whether any array element along a given axis evaluates to True.

#### Syntax:

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

#### Parameters:

**array:**Input array whose elements need to be checked.**axis:**Axis along which array elements are evaluated.**out:**Output array having the same dimensions as Input array**keepdmis:**If this is set to True, the axes which are reduced are left in the result.

#### Return Value:

- A new Boolean array (depending on the ‘out;’ parameter)

```
import numpy as np
arr = np.array([])
flag = not np.any(arr)
if flag:
print('Array is empty')
else:
print('Array is not empty')
```

#### Output:

Array is empty

In this example, we have used numpy.any() method to check whether the array is empty or not. As the array is empty, the value of the flag variable becomes True, and so the output ‘Array is empty’ is displayed. The limitation to this function is that it does not work if the array contains the value 0 in it.

## Method 2: numpy.size() to check if the NumPy array is empty in Python using

We use the numpy.size() function in python to count the number of elements along a given axis.

#### Syntax:

numpy.size(arr, axis=None)

#### Parameters:

**arr:****axis:**Axis along which the elements are counted.

#### Return Value:

- The number of elements along the given axis.

```
import numpy as np
arr = np.array([])
flag = np.size(arr)
if flag:
print('Array is not empty')
else:
print('Array is empty')
```

#### Output:

Array is empty

The np.size() method returns 0, as the array is empty. As a result, the else part is executed and the desired output is displayed.

## Method 3: By Converting it to list:

```
import numpy as np
a = np.array([])
if len(a.tolist()) == 0:
print("Empty")
```

#### Output:

Empty

In this example, we first converted the array into a list using the tolist() method. We then checked the size of the list using the len() method to check whether the array is empty.

## Method 4: Using arr.size

```
import numpy as np
a = np.array([])
if a.size == 0:
print("Empty")
```

#### Output

Empty

In this example, we have used the arr.size operator to check if the array is empty or not. This operator gives the size of the array, which in this case is 0, thus giving the desired output.

## Method 5: Using arr.shape() method

This is an attribute of the numpy array that returns a tuple giving the shape of the array. We can use this to check if the array is empty.

```
import numpy as np
a = np.array([])
if a.shape[0] == 0:
print("Empty")
```

#### Output:

Empty

In this example, we used the arr.shape attribute of the NumPy array. We checked if the number of elements in the 0th axis, i.e., row, is zero or not.

**Also Read:** How to Check If List is Empty in Python With Examples

## Conclusion:

With this, we come to an end with this article. These are the few ways to check if the NumPy array is empty or not in Python.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

*Happy Pythoning!*