w3resource

NumPy: Test whether all elements in an array evaluate to True

NumPy: Array Object Exercise-23 with Solution

Write a NumPy program to test whether all elements in an array evaluate to True.
Note: 0 evaluates to False in python.

Pictorial Presentation:

Python NumPy: Test whether all elements in an array evaluate to True

Sample Solution:-

Python Code:

import numpy as np
print(np.all([[True,False],[True,True]]))
print(np.all([[True,True],[True,True]]))
print(np.all([10, 20, 0, -50]))
print(np.all([10, 20, -50]))

Sample Output:

False                                                                  
True                                                                   
False                                                                  
True

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to find the union of two arrays. Union will return the unique, sorted array of values that are in either of the two input arrays.
Next: Write a NumPy program to test whether any array element along a given axis evaluates to True.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

List comprehension:

>>> m = [x ** 2 for x in range(5)]
>>> m
[0, 1, 4, 9, 16]