np.any() mirrors np.all() exactly, but checks for at least one truthy element rather than requiring every element to be truthy. Without an axis, it collapses the whole array to a single True/False; with an axis specified on a multi-dimensional array, it returns a boolean array indicating whether at least one element was truthy along that collapsed dimension. It's a common, fast way to check for the presence of a condition anywhere in a dataset, like checking whether any values are missing or negative.
1Understanding np.any()
np.any() mirrors np.all() exactly, but checks for at least one truthy element rather than requiring every element to be truthy. Without an axis, it collapses the whole array to a single True/False; with an axis specified on a multi-dimensional array, it returns a boolean array indicating whether at least one element was truthy along that collapsed dimension. It's a common, fast way to check for the presence of a condition anywhere in a dataset, like checking whether any values are missing or negative.
np.any(np.isnan(arr)) is the standard, fast way to check whether an array contains any NaN values at all, without needing to know their positions.
import numpy as np
arr = np.array([1, -2, 3, 4])
print(np.any(arr < 0))2Practical Example
Here is a real-world application of np.any() showing how it is used in production NumPy code.
import numpy as np
data = np.array([1.0, np.nan, 3.0])
print(np.any(np.isnan(data)))3Best Practices
Follow these guidelines when working with np.any():
1. Use np.any(condition) to quickly check whether a condition holds anywhere in an array, instead of looping with an early break
2. Combine np.any() with np.isnan()/np.isinf() to check for problematic values before running further calculations
3. Specify axis explicitly for per-row or per-column checks on multi-dimensional data, rather than only a single whole-array result
Tip: np.any(np.isnan(arr)) is the standard, fast way to check whether an array contains any NaN values at all, without needing to know their positions.
import numpy as np
arr = np.array([1, -2, 3, 4])
print(np.any(arr < 0))