🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.any()

AI & DATA SCIENCE // np-any

np.any() tests whether at least one element of an array is truthy, either overall or along a specified axis, returning a single boolean or a boolean array.

Syntax

np.any(arr, axis=None)

Deep Dive Course

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.

editor.html
import numpy as np

arr = np.array([1, -2, 3, 4])
print(np.any(arr < 0))
localhost:3000

2Practical Example

Here is a real-world application of np.any() showing how it is used in production NumPy code.

editor.html
import numpy as np

data = np.array([1.0, np.nan, 3.0])
print(np.any(np.isnan(data)))
localhost:3000

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.

editor.html
import numpy as np

arr = np.array([1, -2, 3, 4])
print(np.any(arr < 0))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([1, -2, 3, 4])
print(np.any(arr < 0))
Example 02Advanced Example
import numpy as np

data = np.array([1.0, np.nan, 3.0])
print(np.any(np.isnan(data)))

Best Practices

  • Use np.any(condition) to quickly check whether a condition holds anywhere in an array, instead of looping with an early break
  • Combine np.any() with np.isnan()/np.isinf() to check for problematic values before running further calculations
  • Specify axis explicitly for per-row or per-column checks on multi-dimensional data, rather than only a single whole-array result

Interview Question

Why is np.any(np.isnan(arr)) preferred over trying to check whether np.nan is directly a member of arr?

Hint: Think about how NaN compares to itself and to everything else.

NaN is specifically defined to never equal anything, including another NaN, so a membership check would actually rely on equality comparisons internally that always fail for NaN, making it an unreliable way to detect NaN values — it can report NaN as absent even when it's genuinely present. np.isnan(arr) instead checks each element's specific bit pattern for the NaN representation directly, rather than relying on equality, correctly identifying NaN values regardless of the fact that NaN never equals itself; wrapping that in np.any() then checks if any such value exists anywhere in the array.

Exercises

MediumPractice using np.any() in a real scenario.
View Solution
import numpy as np

arr = np.array([1, -2, 3, 4])
print(np.any(arr < 0))

Frequently Asked Questions

Why is np.any(np.isnan(arr)) preferred over trying to check whether np.nan is directly a member of arr?

NaN is specifically defined to never equal anything, including another NaN, so a membership check would actually rely on equality comparisons internally that always fail for NaN, making it an unreliable way to detect NaN values — it can report NaN as absent even when it's genuinely present. np.isnan(arr) instead checks each element's specific bit pattern for the NaN representation directly, rather than relying on equality, correctly identifying NaN values regardless of the fact that NaN never equals itself; wrapping that in np.any() then checks if any such value exists anywhere in the array.

Related Functions

np-allnp-isnanboolean-indexing