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
Answer from JoshAdel on Stack Overflow Top answer 1 of 4
487
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
2 of 4
32
One caveat, though.
Note that np.array(None).size returns 1!
This is because a.size is equivalent to np.prod(a.shape),
np.array(None).shape is (), and an empty product is 1.
>>> import numpy as np
>>> np.array(None).size
1
>>> np.array(None).shape
()
>>> np.prod(())
1.0
Therefore, I use the following to test if a NumPy array has elements:
>>> def elements(array):
... return array.ndim and array.size
>>> elements(np.array(None))
0
>>> elements(np.array([]))
0
>>> elements(np.zeros((2,3,4)))
24
TutorialsPoint
tutorialspoint.com โบ what-is-the-preferred-method-to-check-for-an-empty-array-in-numpy
What is the preferred method to check for an empty array in NumPy?
# importing NumPy module with an ... true temp_flag = np.any(inputArray) # checking whether the temp_flag is false (Numpy array is empty) if temp_flag == False: # printing empty array, if the condition is true print('Empty NumPy Input Array') else: # else printing NOT ...
Videos
w3resource
w3resource.com โบ python-exercises โบ numpy โบ python-numpy-exercise-95.php
NumPy: Check whether the numpy array is empty or not - w3resource
# Importing the NumPy library and aliasing it as 'np' import numpy as np # Creating a NumPy array 'x' containing integers [2, 3] x = np.array([2, 3]) # Creating an empty NumPy array 'y' y = np.array([]) # Printing the size of array 'x' # As 'x' contains 2 elements, its size is 2 print(x.size) # Printing the size of array 'y' # 'y' is an empty array, so its size is 0 print(y.size)
IncludeHelp
includehelp.com โบ python โบ how-to-check-whether-a-numpy-array-is-empty-or-not.aspx
How to check whether a NumPy array is empty or not?
If the given array is not empty it returns True; False, otherwise. numpy.any(a, axis=None, out=None, keepdims=<no value>, *, where=<no value>) Consider the below Python program to check NumPy array is empty or not using numpy.any() method.
NumPy
numpy.org โบ doc โบ 2.1 โบ reference โบ generated โบ numpy.empty.html
numpy.empty โ NumPy v2.1 Manual
Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None. ... Return an empty array with shape and type of input.
NumPy
numpy.org โบ doc โบ stable โบ reference โบ generated โบ numpy.empty.html
numpy.empty โ NumPy v2.4 Manual
Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None. ... Return an empty array with shape and type of input.
NumPy
numpy.org โบ devdocs โบ reference โบ generated โบ numpy.empty.html
numpy.empty โ NumPy v2.5.dev0 Manual
Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None. ... Return an empty array with shape and type of input.
Python Guides
pythonguides.com โบ check-if-numpy-array-is-empty
Check If NumPy Array Is Empty In Python
May 16, 2025 - Let me share a real-world example where checking for empty arrays is crucial. Imagine weโre processing financial transaction data for a US-based retail company: import numpy as np def process_sales_data(daily_transactions): """Process daily sales transactions, skipping days with no data.""" total_revenue = 0 processed_days = 0 for day, transactions in enumerate(daily_transactions, 1): # Skip processing if no transactions for the day if transactions.size == 0: print(f"Day {day}: No transactions recorded, skipping analysis") continue daily_revenue = np.sum(transactions) total_revenue += daily_
NumPy
numpy.org โบ doc โบ 1.25 โบ reference โบ generated โบ numpy.empty.html
numpy.empty โ NumPy v1.25 Manual
Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None. ... Return an empty array with shape and type of input.
Numpywhere
numpywhere.com โบ check-if-numpy-array-is-empty.html
Check if Numpy Array is Empty|NumpyWhere
In the above code, arr1 is empty, ... value, so the expression evaluates to False. The np.inf value represents infinity in Numpy. By using the np.isfinite() function, we can check if an array contains infinite values or is empty....
GitHub
github.com โบ numpy โบ numpy โบ issues โบ 9583
Deprecate truth-testing on empty arrays ยท Issue #9583 ยท numpy/numpy
August 19, 2017 - I tested the waters with this on the numpy-discussion newsgroup earlier this week yesterday, and the general response seemed to be that this is actionable, so I am making an issue for further discussion. The long and short is that truth-...
Published ย Aug 19, 2017
NumPy
numpy.org โบ doc โบ 2.0 โบ reference โบ generated โบ numpy.empty.html
numpy.empty โ NumPy v2.0 Manual
Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None. ... Return an empty array with shape and type of input.
Enterprise DNA
blog.enterprisedna.co โบ python-check-if-list-is-empty-7-methods-explained
Python: Check If List is Empty (7 Methods Explained) โ Master Data Skills + AI
import numpy as np arr = np.array([]) # An empty numpy array if arr.size == 0: print("The numpy array is empty.") else: print("The numpy array is not empty.")
Verve AI
vervecopilot.com โบ interview-questions โบ why-is-understanding-an-empty-numpy-array-crucial-for-your-next-technical-interview
Why Is Understanding An Empty Numpy Array Crucial For Your Next Technical Interview?
The primary function for creating an uninitialized empty NumPy array is np.empty(). Notice that arr_uninitialized will display seemingly random numbers. These are not zeros; they are whatever values were in the memory segment before NumPy allocated it. # Check the size of arrays print(f"\nSize of arr_uninitialized: {arr_uninitialized.size}") # Output: 6 print(f"Size of arr_zero_elements_1: {arr_zero_elements_1.size}") # Output: 0 print(f"Size of arr_zero_elements_2: {arr_zero_elements_2.size}") # Output: 0 <h1>A function to check if an array has zero elements</h1> <p>def is_array_empty(arr):<br> return arr.size == 0</p>
NumPy
numpy.org โบ doc โบ 2.2 โบ reference โบ generated โบ numpy.empty.html
numpy.empty โ NumPy v2.2 Manual
Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None. ... Return an empty array with shape and type of input.