Python:NumPy any()

design8615833887's avatar
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.

  • 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

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 default None evaluates 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): If True, 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 values
arr1 = np.array([0, 1, 2, 3])
print(arr1.any())
# 1D array with all zeros
arr2 = np.array([0, 0, 0])
print(arr2.any())
# 2D array with axis parameter
arr3 = 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 array
arr4 = np.array([[True, False], [False, False]])
print(arr4.any())

The output for the above code will be:

True
False
Along 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:

Code
Output
Loading...

All contributors

Contribute to 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