← Back to NumPy Course | Chapter 10: Boolean & Comparison | Lesson 3 of 6

np.any/all

any asks "is at least one True?" and all asks "is every one True?".

In this page:

  1. np.any/all
Syntax
python
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

python
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
  1. Using Python's any and all on 2-D arrays
  2. Forgetting non-zero is True
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.