How to Check if a NumPy Array Is Empty in Python (size vs len)

To check if a NumPy array is empty, test arr.size == 0. size is the total number of elements, so it is 0 only when the array holds nothing, whatever its shape. Don’t use if not arr: (it raises an error for arrays with more than one element), and be careful with len(arr), which only counts the first dimension. For a plain Python list, if not my_list: is the usual check.

Every example was run with Python 3.12.5 and NumPy 2.5.3; the output shown is from those runs.

import numpy as np

arr = np.array([])

if arr.size == 0:
    print("The array is empty")

Output:

The array is empty

size vs len() vs shape

import numpy as np

arrays = {
    "np.array([])": np.array([]),
    "np.array([5])": np.array([5]),
    "np.array([0, 0])": np.array([0, 0]),
    "np.zeros((0, 3))": np.zeros((0, 3)),
    "np.zeros((3, 0))": np.zeros((3, 0)),
    "np.zeros((2, 3))": np.zeros((2, 3)),
}
for label, a in arrays.items():
    print(f"{label:18} shape={str(a.shape):8} size={a.size:<2} len={len(a):<2} empty={a.size == 0}")

Output:

np.array([])       shape=(0,)     size=0  len=0  empty=True
np.array([5])      shape=(1,)     size=1  len=1  empty=False
np.array([0, 0])   shape=(2,)     size=2  len=2  empty=False
np.zeros((0, 3))   shape=(0, 3)   size=0  len=0  empty=True
np.zeros((3, 0))   shape=(3, 0)   size=0  len=3  empty=True
np.zeros((2, 3))   shape=(2, 3)   size=6  len=2  empty=False
Table showing shape, size and len for NumPy arrays including np.array([]), np.zeros((0, 3)) and np.zeros((3, 0)), and whether each array is empty
size is 0 exactly when the array is empty. len() can mislead for 2-D arrays.

The 3×0 array is the trap: it has 3 rows but no elements, so len() says 3 while size correctly says 0:

import numpy as np

a = np.zeros((3, 0))       # 3 rows, 0 columns: no elements at all
print("len(a):", len(a))   # counts rows only
print("a.size:", a.size)   # counts every element

Output:

len(a): 3
a.size: 0
Command Prompt screenshot: len() returns 3 but size returns 0 for a NumPy array with shape (3, 0)
Run in the Command Prompt: len() says 3, size says 0.
CheckEmpty 1-D array3×0 arrayRecommended?
arr.size == 0TrueTrueYes
len(arr) == 0TrueFalseOnly for 1-D arrays
arr.shape[0] == 0TrueFalseSame problem as len()
not arrErrorErrorNo

Why if not arr doesn’t work

import numpy as np

arr = np.array([1, 2, 3])
if not arr:
    print("empty")

Output:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

An array with several elements has no single True/False value, so NumPy refuses to guess. Recent NumPy versions also refuse an empty array:

import numpy as np

arr = np.array([])
print(bool(arr))

Output:

ValueError: The truth value of an empty array is ambiguous. Use `array.size > 0` to check that an array is not empty.
Command Prompt screenshot of the ValueError: The truth value of an empty array is ambiguous, raised by bool() on an empty NumPy array
The real traceback from NumPy 2.5 in the Command Prompt.

A one-element array is the confusing exception: it takes the truth value of that one element, so np.array([0]) is falsy even though it is not empty:

import numpy as np

print(bool(np.array([0])), bool(np.array([7])))   # one element: its own truth value

Output:

False True

There is no np.isempty()

Many people search for numpy isempty, but the function doesn’t exist:

import numpy as np

print(np.isempty(np.array([])))

Output:

AttributeError: module 'numpy' has no attribute 'isempty'. Did you mean: 'empty'?

Use arr.size == 0 (or np.size(arr) == 0) instead. size is documented in the NumPy reference for ndarray.size.

Check if a NumPy array is not empty

import numpy as np

scores = np.array([88, 92, 79])

if scores.size > 0:
    print("scores is not empty; mean =", scores.mean())

Output:

scores is not empty; mean = 86.33333333333333

Don’t confuse “not empty” with “has non-zero values”. any() answers the second question:

import numpy as np

zeros = np.zeros(3)
print("size:", zeros.size, "-> empty?", zeros.size == 0)
print("any():", zeros.any(), "-> any non-zero values?")

Output:

size: 3 -> empty? False
any(): False -> any non-zero values?

np.empty() does not create an empty array

np.empty(3) is a fast way to allocate 3 elements without setting their values. Only a size of 0 gives an empty array (more in NumPy empty array):

import numpy as np

a = np.empty(3)          # allocates 3 elements; does NOT make an empty array
print("np.empty(3) size:", a.size)

b = np.empty(0)          # this one really is empty
print("np.empty(0) size:", b.size, "| np.array([]) size:", np.array([]).size)

Output:

np.empty(3) size: 3
np.empty(0) size: 0 | np.array([]) size: 0

Practical examples

Filtering can return an empty array

import numpy as np

temps = np.array([21.5, 23.0, 19.8])
hot = temps[temps > 30]           # filtering can return an empty array

if hot.size == 0:
    print("No day was above 30 degrees")
else:
    print("Hot days:", hot)

Output:

No day was above 30 degrees

Many reductions fail on an empty array, so check before calling them:

import numpy as np

hot = np.array([])
print(hot.max())

Calling max() on an empty array:

ValueError: zero-size array to reduction operation maximum which has no identity
import numpy as np

def safe_max(a, default=None):
    return a.max() if a.size else default

print(safe_max(np.array([3, 9, 4])), safe_max(np.array([])))

Output:

9 None

Loop while an array is not empty

import numpy as np

queue = np.array([10, 20, 30])
while queue.size:                 # loop until the array is empty
    first, queue = queue[0], queue[1:]
    print("processing", first, "- left:", queue.size)

Output:

processing 10 - left: 2
processing 20 - left: 1
processing 30 - left: 0

An array full of NaN is not empty

import numpy as np

a = np.array([np.nan, np.nan])
print("empty:", a.size == 0)
print("all values missing:", np.isnan(a).all())
print("after dropping NaN, empty:", a[~np.isnan(a)].size == 0)

Output:

empty: False
all values missing: True
after dropping NaN, empty: True

Check if a Python list or array is empty

Python’s built-in list is often called an array. For lists, an empty list is falsy, so if not numbers: works (more in check if a list is empty in Python):

numbers = []                      # a Python list
if not numbers:
    print("list is empty")

numbers.append(5)
if numbers:
    print("list is not empty:", numbers)

Output:

list is empty
list is not empty: [5]

The standard-library array module behaves the same way:

from array import array

a = array("i")                    # array module: typed array of ints
print("empty:", not a, "| len:", len(a))
a.append(7)
print("empty:", not a, "| len:", len(a))

Output:

empty: True | len: 0
empty: False | len: 1

One check for any kind of array

np.size() works on lists, tuples and NumPy arrays alike:

from array import array
import numpy as np

def is_empty(x):
    """Works for NumPy arrays, lists, tuples and array.array."""
    return np.size(x) == 0

for x in [np.array([]), np.zeros((3, 0)), [], [1, 2], (), (0,), array("i"), array("i", [7])]:
    print(f"{x!r:40} -> {is_empty(x)}")

Output:

array([], dtype=float64)                 -> True
array([], shape=(3, 0), dtype=float64)   -> True
[]                                       -> True
[1, 2]                                   -> False
()                                       -> True
(0,)                                     -> False
array('i')                               -> True
array('i', [7])                          -> False
Command Prompt screenshot of a Python is_empty function using np.size that works for NumPy arrays, lists, tuples and array.array
One np.size() check for arrays, lists, tuples and array.array.

Keep exploring NumPy and empty checks:

Frequently asked questions

How do I check if a NumPy array is empty?

Use arr.size == 0. It counts all elements, so it works for any number of dimensions.

Is there a numpy isempty() function?

No. np.isempty raises AttributeError. Use arr.size == 0 or np.size(arr) == 0.

Why does if not arr raise a ValueError?

An array with more than one element has no single truth value, so NumPy raises “The truth value of an array with more than one element is ambiguous”. Recent versions also raise for empty arrays.

Is len(arr) == 0 a good way to check?

Only for 1-D arrays. len() counts rows, so a 3×0 array has len() 3 but no elements.

How do I check if a NumPy array is not empty?

arr.size > 0, or simply if arr.size:.

How do I check if a Python list is empty?

if not my_list: is True for an empty list. len(my_list) == 0 works too.