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?
October 20, 2022 - This can be used to see if the array is empty. The following program returns whether the given NumPy array is empty or not using the shape attribute ? import numpy as np # creating a numpy array inputArray = np.array([]) # checking whether the shape of the array is equal to 0 (Empty array condition) if inputArray.shape[0] == 0: # prining empty array if condition is true print("Empty input array") else: # else printing not Empty array print('Input array is NOT empty')
Deprecate truth-testing on empty arrays
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 discu... More on github.com
How can I create a truly empty numpy array which can be merged onto (by a recursive function)?
I can't say I fully followed your problem statement, but you can create an array with a total size of zero if any of the dimensions has size zero: a = np.empty((0, 3)) # Doesn't really matter if you use `empty`, `zeros` or `ones` here Zero-size arrays are the neutral element wrt. concatenation along their zero-size dimension (if that's what you mean by "merging"): b = np.random.uniform(size=(20, 3)) c = np.concatenate([a, b], 0) (c == b).all() # True More on reddit.com
Is it possible to grab the last element of a numpy array with a negative slice?
You can't loop from negative to postive slice values as they don't "wrap". a[-2:2] would also give an empty array. More on reddit.com
Why does numpy.empty put numbers on the order of 1^9 or 1^(-300) in the array?
It's allocating the memory without initializing. The memory contains whatever was already there, which could be bits of a program or a string or anything at all. Interpreting those random bit patterns as numbers, it's not surprising they might happen to correspond to very large or very small exponents. So it's not "using" any values. That's just the numeric value that's displayed when the array element happened to be the 47,042-th pixel in the picture of somebody's cat. More on reddit.com
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-95.php
NumPy: Check whether the numpy array is empty or not - w3resource
August 29, 2025 - # 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) ... print(x.size): Print the size of array 'x' using the size attribute.
NumPy
numpy.org › doc › 1.25 › reference › generated › numpy.empty.html
numpy.empty — NumPy v1.25 Manual
Return an empty array with shape and type of input. ... Return a new array setting values to one. ... Return a new array setting values to zero.
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?
May 26, 2023 - To check an empty NumPy array, there are multiple methods that you can use such as numpy.ndarray.size attribute, numpy.any() method, and numpy.size() method. Let's discuss all these methods with examples. The ndarray.size attribute returns the total number of elements in a NumPy array.
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.3 Manual
For reproducible behavior, be sure to set each element of the array before reading. ... Try it in your browser! >>> import numpy as np >>> np.empty([2, 2]) array([[ -9.74499359e+001, 6.69583040e-309], [ 2.13182611e-314, 3.06959433e-309]]) #uninitialized
GeeksforGeeks
geeksforgeeks.org › numpy › python-numpy
Python NumPy - GeeksforGeeks
The number of dimensions is called the rank and the size along each dimension is called the shape. In NumPy, arrays are called ndarray and elements are accessed using square brackets [], often created from nested Python lists.
Published 2 weeks ago
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?
Before performing any aggregations (sum, mean, max) or indexing on a NumPy array, especially if it's user-provided or dynamically generated, always check its .size attribute. This simple check prevents runtime errors and shows your commitment to writing robust code [4]. When discussing array initialization, clearly explain that np.empty() is for uninitialized memory and is faster, while np.zeros() explicitly fills with zeros [2]. Be prepared to discuss scenarios where one might be preferred over the other (e.g., pre-allocating for iterative filling vs.
NumPy
numpy.org › doc › 2.3 › user › absolute_beginners.html
NumPy: the absolute basics for beginners — NumPy v2.3 Manual
If the element you’re looking for doesn’t exist in the array, then the returned array of indices will be empty.
W3Schools
w3schools.com › python › numpy › numpy_array_filter.asp
NumPy Filter Array
The above example is quite a common task in NumPy and NumPy provides a nice way to tackle it. We can directly substitute the array instead of the iterable variable in our condition and it will work just as we expect it to. Create a filter array that will return only values higher than 42: import numpy as np arr = np.array([41, 42, 43, 44]) filter_arr = arr > 42 newarr = arr[filter_arr] print(filter_arr) print(newarr) Try it Yourself »
Python Guides
pythonguides.com › check-if-an-array-is-empty-in-python
How To Check If An Array Is Empty In Python?
March 19, 2025 - You can use the size the attribute of a NumPy array to determine if it is empty.
w3resource
w3resource.com.cach3.com › python-exercises › numpy › python-numpy-exercise-95.php.html
NumPy: Check whether the numpy array is empty or not - w3resource
May 28, 2022 - import numpy as np x = np.array([2, 3]) y = np.array([]) # size 2, array is not empty print(x.size) # size 0, array is empty print(y.size) ... Have another way to solve this solution? Contribute your code (and comments) through Disqus. Previous: Write a NumPy program to count the frequency of unique values in numpy array. Next: Write a NumPy program to divide each row by a vector element. ... Test your Programming skills with w3resource's quiz. ... (Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)
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
Educative
educative.io › answers › how-to-create-an-empty-numpy-array
How to create an empty NumPy array
numpy.zeros(shape, dtype=float, order='C') numpy.empty(shape, dtype=float, order='C') # Shape -> Shape of the new array, e.g., (2, 3) or 2. # dtype -> The desired data-type for the array,e.g., numpy.int8. Default is numpy.float64. This parameter is optional. # order -> Indicates whether multi-dimensional data should be stored in row-major (C-style) or column-major (Fortran-style) order in memory.
W3Schools
w3schools.com › python › numpy › numpy_creating_arrays.asp
NumPy Creating Arrays
NumPy is used to work with arrays.