np.any/all
any asks "is at least one True?" and all asks "is every one True?".
In this page:
Syntax
np.any(condition_array)
np.all(condition_array)
np.any/all
np.any returns True if any element is True; np.all returns True only if all are. Both take an axis argument to test rows or columns. Non-zero numbers count as True.
Note:
Use np.all(a == b) or array_equal to check two arrays match.
Example: np.any/all
import numpy as np
a = np.array([[1, 2], [0, 4]])
print(np.any(a == 0), np.all(a > 0))
print(np.all(a > 0, axis=1))
print(np.any(a > 3, axis=0))
# Output:
# True False
# [ True False]
# [False True]
Related Topics
Common Mistakes
- Using Python's any and all on 2-D arrays
- Forgetting non-zero is True
- Testing an empty array and misjudging the result
Chapter Summary
- any needs at least one True
- all needs every element True
- axis tests rows or columns
- all of an empty array is True
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: