Open In App

How to check whether the elements of a given NumPy array is non-zero?

Last Updated : 29 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

In NumPy with the help of any() function, we can check whether any of the elements of a given array in NumPy is non-zero. We will pass an array in the any() function if it returns true then any of the element of the array is non zero if it returns false then all the elements of the array are zero.

Syntax: numpy.any()

Parameter :An array.

Return :It return boolean value(True/False).

Example 1:

Python




import numpy as np
  
  
# Original array
array = np.array([4,6,0,0,0,4,89])
print(x)
  
# Test whether any of the elements
# of a given array is non-zero
print(np.any(array))


Output:

True

Example 2:

Python




import numpy as np
  
# Original array
array = np.array([0,0,0,0,0,0])
print(x)
  
# Test whether any of the elements
# of a given array is non-zero
print(np.any(array))


Output:

False

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads