np.any: What is numpy any() Function in Python

Numpy any() function is used to check whether an array element along the mentioned axis evaluates True or False. If an element in a particular axis is True, it returns True.


The np.any() function returns True when ndarray passed to the first parameter contains at least one True element and returns False otherwise. For example, if you specify the parameter axis, it returns True if at least one element is True for each axis.


numpy.any(array, axis = None, out = None, keepdims = <NoValue>)


Numpy any() function takes up to four parameters:

  1. array: This is the array on which we need to work.
  2. axis: Axis or axes around which is done a logical reduction of OR. The default (axis = None) executes logical OR overall input array dimensions. Axis may be negative, in which case it is counted from the last axis to the first. If this is a tuple of ints, there is a reduction on multiple axes instead of a single axis or all of the axes as before. 
  3. out: This is an optional field. Alternate output array to position the result into. It must have the same shape as the planned performance and maintain its form.
  4. keepdims: If this is set to True, the reduced axes are left as the dimensions with size one. With this option, the result will broadcast correctly against an input array.

If the default value is passed, then the keepdims will not be passed through to any method of sub-classes of ndarray.

Any exceptions will be raised if the sub-class’ method does not implement the keepdims.

Return Value

The any() function always returns a Boolean value. But this boolean value depends on the ‘out’ parameter.

Please note that Not a Number (NaN), positive infinity, and negative infinity are evaluated to True as they are not equal to zero.

Program to show the working of any()

See the following code.

import numpy as np
#Declaring different types of array

arr1 = [[True, False], [True, False]]
print(np.any(arr1, axis=0))

arr2 = [5, 10, 0, 100]



[ True False]


In this example, we have declared different types of an array and tested the output in each case.

At first, we have declared a 2D array containing True and False.

Then we have called any() and gave axis =1, it checks column-wise, and as in the first column, there is one True, the result is True, and in the second column, both are False, so the output is False.

We have one 1D array containing only positive numbers; as the number is not 0, the result is True. In the third case, the array value is nan; as mentioned above, the output is True.

See also

Numpy nanargmax()

Numpy nanargmin()

Numpy argmin()

Numpy argmax()

Numpy take()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.