To check if all the values in a Numpy array are zero or not, you can use a combination of the equality operator ==
and the all()
function. The idea is to compare the array with 0
using the ==
operator and check if all the values in the resulting boolean array are True
or not using the all
function.
The following is the syntax –
import numpy as np # check if numpy array is all zero (ar == 0).all()
There are other methods as well, for example –
- Convert the array to a set and check if the set contains only 0.
- Iterate through the array and return
False
if you encounter any non-zero value.
Let’s now look at the methods mentioned above with the help of some examples.
Example 1 – Check if Array is all zero using all()
function
If you compare an array with a scaler value, the resulting array would be a boolean array with True
for the array values that were equal to the scaler value and False
otherwise.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
In this method, we compare the array with 0
and check if all the values in the resulting boolean array are True
or not using the numpy array all()
function.
Let’s look at an example
import numpy as np # create two arrays ar1 = np.array([0, 0, 0, 0]) ar2 = np.array([0, None, 0, 0]) # check if array is all zero print((ar1 == 0).all()) print((ar2 == 0).all())
Output:
True False
Here, we created two arrays. ar1
with all values as 0
and ar2
with only some values as 0
(not all) and then check if these arrays are all zero or not using our method.
We get True
for ar1
and False
for ar2
, which is the correct result.
Example 2 – Using a set
The idea here is that if the array has only 0
values, then the only unique value in the array should be 0
. We can get the unique values in an array by converting it to a set. Let’s look at an example.
import numpy as np # create two arrays ar1 = np.array([0, 0, 0, 0]) ar2 = np.array([0, None, 0, 0]) # check if array is all zero print((len(set(ar1)) == 1) and (0 in set(ar1))) print((len(set(ar2)) == 1) and (0 in set(ar2)))
Output:
True False
We get the same result as above.
Example 3 – Iterate through the array
Alternatively, we can iterate through the entire array, element by element, and check if each element is 0
or not. If we encounter a non-zero element, we return False
.
import numpy as np # create two arrays ar1 = np.array([0, 0, 0, 0]) ar2 = np.array([0, None, 0, 0]) # function to check if array is all zero def is_array_all_zero(ar): if len(ar) == 0: return False for val in ar: if val == 0: continue else: return False return True # check if array is all zero print(is_array_all_zero(ar1)) print(is_array_all_zero(ar2))
Output:
True False
We get the same result as above. You can think of this method as a more verbose version (and less optimized) version of method 1.
You might also be interested in –
- Numpy – Check If Array has any Duplicates
- Numpy – Check If Array is Monotonically Decreasing
- Numpy – Check If Array is Monotonically Increasing
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.