6 Ways to check if all values in Numpy Array are zero (in both 1D & 2D arrays) – Python

Check if all values in Numpy Array are zero (in both 1D & 2D arrays) in Python

In this article we will discuss about different ways to  check if all values in a numpy array are 0 i.e in both 1D and 2D arrays.

So let’s start exploring the topic.

Method 1: Using numpy.all() to check if a 1D Numpy array contains only 0 :

In this method we will check that each element of the array will be compared with the particular element i.e zero. And a a result it will return a bool array containing True or False.

# program :

import numpy as np

# 1D numpy array created from a list
arr = np.array([0, 0, 0, 0, 0, 0])

# Checking if all elements in array are zero
check_zero = np.all((arr == 0))
if check_zero:
    print('All the elements of array are zero')
else:
    print('All the elements of the array are not zero')
Output :
All the elements of array are zero

Method 2: Using numpy.any() to check if a 1D Numpy array contains only 0 :

We can use the numpy.any() function to check that if the array contains only zeros by looking for any non zero value. All the elements received by numpy.any() function gets typecast to bool values i.e. 0 to False and others as True. So if all the values in the array are zero then it will return False and then by using not we can confirm our array contains only zeros or not.

# program :

import numpy as np

# 1D numpy array created from a list
arr = np.array([0, 0, 0, 0, 0, 0])

# Checking if all elements in array are zero
check_zero = not np.any(arr)
if check_zero:
    print('All the elements of array are zero')
else:
    print('All the elements of the array are not zero')
Output : 
All the elements of array are zero

Method 3: Using numpy.count_nonzero() to check if a 1D Numpy array contains only 0 :

numpy.count_nonzero()  function returns a count of non-zero values in the array. So by using it we can check if the array contains any non zero value or not. If any non zero element found in the array then all the elements of the array are not zero and if no non zero value found then then all the elements in the array are zero.

# program :

import numpy as np

# 1D numpy array created from a list
arr = np.array([0, 0, 0, 0, 0, 0])

# Checking if all elements in array are zero
# Count non zero items in array
count_non_zeros = np.count_nonzero(arr)
if count_non_zeros==0:
    print('All the elements of array are zero')
else:
    print('All the elements of the array are not zero')
Output :
All the elements of array are zero

Method 4: Using for loop to check if a 1D Numpy array contains only 0 :

By iterating over all the elements of the array we also check that array contains only zeros or not.

# program :

import numpy as np

# 1D numpy array created from a list
arr = np.array([0, 0, 0, 0, 0, 0])

# Checking if all elements in array are zero
def check_zero(arr):
    # iterating of the array
    # and checking if any element is not equal to zero
    for elem in arr:
        if elem != 0:
            return False
    return True
result = check_zero(arr)

if result:
    print('All the elements of array are zero')
else:
    print('All the elements of the array are not zero')
Output :
All the elements of array are zero

Method 5: Using List Comprehension to check if a 1D Numpy array contains only 0 :

By using List Comprehension also we can iterate over each element in the numpy array and then we can create a list of values which are non zero.And if the list contains any element then we can confirm all the values of the numpy array were not zero.

# program :

import numpy as np

# 1D numpy array created from a list
arr = np.array([0, 0, 0, 0, 0, 0])

# Iterating over each element of array 
# And create a list of non zero items from array
result = len([elem for elem in arr if elem != 0])
# from this we can knoew that if our list contains no element then the array contains all zero values.

if result==0:
    print('All the elements of array are zero')
else:
    print('All the elements of the array are not zero')
Output :
All the elements of array are zero

Method 6: Using min() and max() to check if a 1D Numpy array contains only 0 :

If the minimum and maximum value in the array are same and i.e zero then we can confirm the array contains only zeros.

# program :

import numpy as np

# 1D numpy array created from a list
arr = np.array([0, 0, 0, 0, 0, 0])

if arr.min() == 0 and arr.max() == 0:
    print('All the elements of array are zero')
else:
    print('All the elements of the array are not zero')
Output :
All the elements of array are zero

Check if all elements in a 2D numpy array or matrix are zero :

Using the first technique that is by using numpy.all() function we can check the 2D array contains only zeros or not.

# program :

import numpy as np

# 2D numpy array created 
arr_2d = np.array([[0, 0, 0],
                   [0, 0, 0],
                   [0, 0, 0]])

# Checking if all 2D numpy array contains only 0
result = np.all((arr_2d == 0))

if result:
    print('All elemnets of the 2D array are zero')
else:
    print('All elemnets of the 2D array are not zero')
Output : 
All the elements of the 2D array are zero