🚀 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.all()

AI & DATA SCIENCE // np-all

np.all() tests whether every element of an array is truthy, either overall or along a specified axis, returning a single boolean or a boolean array.

Syntax

np.all(arr, axis=None)

Deep Dive Course

Without an axis argument, np.all(arr) collapses the entire array into a single True/False, mirroring Python's built-in all() but vectorized and axis-aware for multi-dimensional arrays. With an axis specified, it instead returns a boolean array, one value per remaining position, indicating whether every element along that collapsed axis was truthy. It's commonly combined with a comparison, like checking that every element is greater than 0, to check a condition holds across an entire array or specific rows/columns.

1Understanding np.all()

Without an axis argument, np.all(arr) collapses the entire array into a single True/False, mirroring Python's built-in all() but vectorized and axis-aware for multi-dimensional arrays. With an axis specified, it instead returns a boolean array, one value per remaining position, indicating whether every element along that collapsed axis was truthy. It's commonly combined with a comparison, like checking that every element is greater than 0, to check a condition holds across an entire array or specific rows/columns.

💡

Use a comparison inside np.all(), like np.all(arr == arr[0]), to check whether every element in an array equals a specific value, or np.array_equal() to compare two whole arrays for exact equality, rather than looping manually.

editor.html
import numpy as np

arr = np.array([1, 2, 3, 4])
print(np.all(arr > 0))
localhost:3000

2Practical Example

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

editor.html
import numpy as np

matrix = np.array([[1, 2], [3, -4]])
print(np.all(matrix > 0, axis=1))
localhost:3000

3Best Practices

Follow these guidelines when working with np.all():

1. Combine np.all() with a comparison to vectorize a condition check across an entire array or axis, instead of looping

2. Specify the axis argument explicitly on multi-dimensional data to check a condition per-row or per-column, rather than only for the whole array

3. Use np.array_equal(a, b) instead of np.all(a == b) when comparing two whole arrays, since array_equal() also correctly handles differing shapes

⚠️

Tip: Use a comparison inside np.all(), like np.all(arr == arr[0]), to check whether every element in an array equals a specific value, or np.array_equal() to compare two whole arrays for exact equality, rather than looping manually.

editor.html
import numpy as np

arr = np.array([1, 2, 3, 4])
print(np.all(arr > 0))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

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

matrix = np.array([[1, 2], [3, -4]])
print(np.all(matrix > 0, axis=1))

Best Practices

  • Combine np.all() with a comparison to vectorize a condition check across an entire array or axis, instead of looping
  • Specify the axis argument explicitly on multi-dimensional data to check a condition per-row or per-column, rather than only for the whole array
  • Use np.array_equal(a, b) instead of np.all(a == b) when comparing two whole arrays, since array_equal() also correctly handles differing shapes

Interview Question

What's the difference between np.all(arr) and Python's built-in all(arr) for a NumPy array?

Hint: Think about performance and axis-awareness for multi-dimensional data.

For a simple 1D array, both produce the same True/False result, but Python's built-in all() works by iterating element by element in a Python-level loop, while np.all() is vectorized and executes at C speed, making it significantly faster for large arrays. np.all() also supports an axis argument for multi-dimensional arrays, letting you check truthiness along a specific dimension and get back an array of results, something the built-in all() has no direct equivalent for.

Exercises

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

arr = np.array([1, 2, 3, 4])
print(np.all(arr > 0))

Frequently Asked Questions

What's the difference between np.all(arr) and Python's built-in all(arr) for a NumPy array?

For a simple 1D array, both produce the same True/False result, but Python's built-in all() works by iterating element by element in a Python-level loop, while np.all() is vectorized and executes at C speed, making it significantly faster for large arrays. np.all() also supports an axis argument for multi-dimensional arrays, letting you check truthiness along a specific dimension and get back an array of results, something the built-in all() has no direct equivalent for.

Related Functions

np-anyboolean-indexingnp-isclose