Python:NumPy any()
Published Oct 31, 2025
Contribute to Docs
The any() method of a NumPy ndarray returns True if at least one element in the array evaluates to True. It performs a logical OR operation across specified array elements. In a Boolean context, all non-zero numbers evaluate to True, while zero evaluates to False.
Syntax
array.any(axis=None, out=None, keepdims=False, where=True)
Parameters:
axis(Optional): Integer or tuple of integers specifying the axis or axes along which the logical OR operation is performed. The defaultNoneevaluates all elements in the array.out(Optional): Output array to place the result. It must have the same shape as the expected output.keepdims(Optional): IfTrue, retains reduced axes as dimensions of size one in the result.where(Optional): Array of booleans specifying which elements to include in the check.
Return value:
Returns a single boolean value or an ndarray of boolean values, depending on the axis parameter.
Example
The following example demonstrates basic usage of the any() method with different array configurations:
import numpy as np# 1D array with non-zero valuesarr1 = np.array([0, 1, 2, 3])print(arr1.any())# 1D array with all zerosarr2 = np.array([0, 0, 0])print(arr2.any())# 2D array with axis parameterarr3 = np.array([[1, 0, 0], [0, 0, 1], [0, 0, 0]])print("Along axis 0:", arr3.any(axis=0))print("Along axis 1:", arr3.any(axis=1))# Boolean arrayarr4 = np.array([[True, False], [False, False]])print(arr4.any())
The output for the above code will be:
TrueFalseAlong axis 0: [ True False True]Along axis 1: [ True True False]True
Codebyte Example
The following codebyte example shows practical applications of the any() method with different scenarios:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours