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

In this article we will discuss seven different ways to check if all values in a numpy array are 0. Then we will look how to find rows or columns with only zeros in a 2D array or matrix.

## Check if all values in a 1D Numpy Array are Zero

First of all, we will import numpy module,

`import numpy as np`

Suppose we have a 1D numpy array,

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

Now we want to confirm that all values in this array are 0. There are different ways to do that, let’s discuss them one by one,

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

We can do this in a single line,

```# Check if all elements in array are zero
is_all_zero = np.all((arr == 0))

if is_all_zero:
print('Array contains only 0')
else:
print('Array has non-zero items too')
```

Output:

`Array contains only 0`

It confirms that array arr contains only zeros.
How did it work?

When we compare a numpy array with a single element in an expression, then that element is compared with each value of the array and this expression returns a bool array, which contains the result of each comparison. So, when we compared our array with 0 i.e.

`bool_arr = (arr == 0)`

It returned a bool array,

```print(bool_arr)
```

Output:

`[True True True True True True]`

As all elements were 0 in the array arr, therefore the returned bool array had only True values. Then we checked if all elements in this bool array were True or not using numpy.all(),

```result = np.all(bool_arr)

print(result)```

Output:

`True`

This is how we confirmed that our numpy array had only zeros.

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

Suppose we have a 1D numpy array of integers,

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

When an integer is typecasted to a bool value, then 0 evaluates to False and all other integers evaluates to True. So, we can directly pass our integer array to numpy.any() which expects a bool array,

```# Check if array contains only zeros by looking for any non zero value
is_all_zero = not np.any(arr)

if is_all_zero:
print('Array contains only 0')
else:
print('Array has non-zero items too')```

Output:

`Array contains only 0`

When function numpy.any() receives an int array as argument, then all values in this integer array arr gets typecast to bool values i.e. 0 to False and others as True. As any() checks if there is any value in the bool array is True or not. So it will return False if all values in the array are 0. Then using not with the returned value, we can confirm if our array contains only 0.

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

`numpy.count_nonzero(a, axis=None)`

numpy.count_nonzero() returns a count of non-zero values in the array arr. We can use to check if array contain only zeros,

```# Count non zero items in array
num_of_non_zeros = np.count_nonzero(arr)

if num_of_non_zeros == 0:
print('Array contains only 0')
else:
print('Array has non-zero items too')```

Output:

`Array contains only 0`

As the count of non-zero values in our array was 0, so it confirms that our array has zeros only.

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

Instead of using any built-in function, we can directly iterate over each element in the array and check if it is 0 or not,

```def check_if_all_zero(arr):
'''
Iterate over the 1D array arr and check if any element is not equal to 0.
As soon as it encounter any element that is not zero, it returns False.
Else in the end it returns True
:param arr: 1D array
:return: True if array contains only 0 else returns False
'''
for elem in arr:
if elem != 0:
return False
return True

result = check_if_all_zero(arr)

if result:
print('Array contains only 0')
else:
print('Array has non-zero items too')```

Output:

`Array contains only 0`

It confirms that all values in our numpy array arr were 0.

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

Just like the previous solution, we can use List Comprehension to iterate over each element in the numpy array and create a list of values which are non zero. Then by checking if the size of the list is 0 or not, we can confirm if all values are zero in our numpy array or not,

```# Iterate over each array and create a list of non zero items from array
result = len([elem for elem in arr if elem != 0])

# If size of new list is 0, then it means our array as only zeros
if result == 0:
print('Array contains only 0')
else:
print('Array has non-zero items too')
```

Output:

`Array contains only 0`

It confirms that all values in our numpy array arr were 0.

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

If the maximum and minimum value in an array are same and that is 0, then it means all values in the array are zeros,

```if arr.min() == 0 and arr.max() == 0:
print('Array contains only 0')
else:
print('Array has non-zero items too')
```

Output:

`Array contains only 0`

It confirms that all values in our numpy array arr were 0.

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

Suppose we have a 2D numpy array,

```arr_2d = np.array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
```

Now we want to check if all values in this 2D Numpy array or matrix are 0. For that we can use the first technique i.e. using numpy.all() and conditional expression,

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

if result:
print('2D Array contains only 0')
else:
print('2D Array has non-zero items too')```

Output:

`Array contains only 0`

It confirms that all values in our numpy array arr were 0. Logic is same i.e. when we compare a single element with 2D array in an expression, then it returns a 2D bool array,

```bool_arr = (arr_2d == 0)

print(bool_arr)```

Output:

```[[ True  True  True]
[ True  True  True]
[ True  True  True]]```

Then using numpy.all() we confirmed if all values in this 2D bool array were True,

```result= np.all(bol_arr)

print(result)```

Output:

`True`

## Find rows & columns with only zeros in a matrix or 2D Numpy array

Suppose we have a 2D numpy array or matrix,

```arr_2d = np.array([[0, 1, 0],
[0, 0, 0],
[0, 0, 0]])
```

Now we want to find all rows and columns which contain only zeros. Let’s see how to do that,

### Find rows with only zeros in a matrix or 2D Numpy array

```# Check row wise
result = np.all((arr_2d == 0), axis=1)

print('Rows that contain only zero:')
for i in range(len(result)):
if result[i]:
print('Row: ', i)
```

Output:

```Rows that contain only zero:
Row:  1
Row:  2```

We iterated over each row of the 2D numpy array and for each row we checked if all elements in that row are zero or not, by comparing all items in that row with the 0.

### Find columns with only zeros in a matrix or 2D Numpy array

```# Check row wise
result = np.all((arr_2d == 0), axis=0)

print('Columns that contain only zero:')
for i in range(len(result)):
if result[i]:
print('Column: ', i)
```

Output:

```Columns that contain only zero:
Column:  0
Column:  2```

We iterated over each column of the 2D numpy array and for each column we checked if all elements in it are zero or not, by comparing all items in that column with the 0.

The complete example is as follows,

```import numpy as np

def check_if_all_zero(arr):
'''
Iterate over the 1D array arr and check if any element is not equal to 0.
As soon as it encounter any element that is not zero, it returns False.
Else in the end it returns True
:param arr: 1D array
:return: True if array contains only 0 else returns False
'''
for elem in arr:
if elem != 0:
return False
return True

def main():

print('**** Check if all values in a Numpy Array are 0 ****')

print('Method 1:')
print('****  Using numpy.all() to check if all values in a 1D Numpy Array are 0 ****')

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

print('1D Numpy array:')
print(arr)

# Check if all elements in array are zero
is_all_zero = np.all((arr == 0))

if is_all_zero:
print('Array contains only 0')
else:
print('Array has non-zero items too')

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

# Check if array contains only zeros by looking for any non zero value
is_all_zero = not np.any(arr)

if is_all_zero:
print('Array contains only 0')
else:
print('Array has non-zero items too')

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

# Count non zero items in array
num_of_non_zeros = np.count_nonzero(arr)

if num_of_non_zeros == 0:
print('Array contains only 0')
else:
print('Array has non-zero items too')

print('method 4:')
print('**** Using for loop Check if a 1D Numpy array contains only 0 ****')

result = check_if_all_zero(arr)

if result:
print('Array contains only 0')
else:
print('Array has non-zero items too')

print('Method 5:')
print('**** Using List Comprehension to check if a 1D Numpy array contains only 0 ****')

# Iterate over each array and create a list of non zero items from array
result = len([elem for elem in arr if elem != 0])

# If size of new list is 0, then it means our array as only zeros
if result == 0:
print('Array contains only 0')
else:
print('Array has non-zero items too')

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

if arr.min() == 0 and arr.max() == 0:
print('Array contains only 0')
else:
print('Array has non-zero items too')

print('**** Check if all elements in a 2D numpy array or matrix are 0 ****')

arr_2d = np.array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])

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

if result:
print('2D Array contains only 0')
else:
print('2D Array has non-zero items too')

print('*** Find rows in a matrix or 2D Numpy array that contains only 0 ***')

arr_2d = np.array([[0, 1, 0],
[0, 0, 0],
[0, 0, 0]])

# Check row wise
result = np.all((arr_2d == 0), axis=1)

print('Rows that contain only zero:')
for i in range(len(result)):
if result[i]:
print('Row: ', i)

print('*** Find columns in a matrix or 2D Numpy array that contains only 0 ***')

# Check row wise
result = np.all((arr_2d == 0), axis=0)

print('Columns that contain only zero:')
for i in range(len(result)):
if result[i]:
print('Column: ', i)

if __name__ == '__main__':
main()```

Output:

```**** Check if all values in a Numpy Array are 0 ****
Method 1:
****  Using numpy.all() to check if all values in a 1D Numpy Array are 0 ****
1D Numpy array:
[0 0 0 0 0 0]
Array contains only 0
Method 2:
**** Using numpy.any() to check if a 1D Numpy array contains only 0 ****
Array contains only 0
Method 3:
**** Using numpy.count_nonzero() to check if a 1D Numpy array contains only ****
Array contains only 0
method 4:
**** Using for loop Check if a 1D Numpy array contains only 0 ****
Array contains only 0
Method 5:
**** Using List Comprehension to check if a 1D Numpy array contains only 0 ****
Array contains only 0
Method 6:
**** Using min() and max() to check if a 1D Numpy array contains only 0 ****
Array contains only 0
**** Check if all elements in a 2D numpy array or matrix are 0 ****
2D Array contains only 0
*** Find rows in a matrix or 2D Numpy array that contains only 0 ***
Rows that contain only zero:
Row:  1
Row:  2
*** Find columns in a matrix or 2D Numpy array that contains only 0 ***
Columns that contain only zero:
Column:  0
Column:  2```

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top