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

AI & DATA SCIENCE // np-argwhere

np.argwhere() finds the indices of all non-zero (or condition-matching) elements, grouped as coordinate rows in a 2D array — one row per matching element.

Syntax

np.argwhere(condition)

Deep Dive Course

np.argwhere(arr) returns a 2D array where each row is the full set of coordinates for one matching element, so for a 2D input array, each row is a (row_index, column_index) pair. This differs structurally from np.where(condition) called with one argument, which instead returns a tuple of separate 1D arrays, one per axis — argwhere() groups the coordinates together per-element, which many people find more directly usable for iterating over specific matching positions.

1Understanding np.argwhere()

np.argwhere(arr) returns a 2D array where each row is the full set of coordinates for one matching element, so for a 2D input array, each row is a (row_index, column_index) pair. This differs structurally from np.where(condition) called with one argument, which instead returns a tuple of separate 1D arrays, one per axis — argwhere() groups the coordinates together per-element, which many people find more directly usable for iterating over specific matching positions.

💡

np.argwhere(condition) and the single-argument form of np.where(condition) find the same matching positions, but structure the result differently — argwhere() groups each match's full coordinates into one row, while where() returns separate per-axis index arrays; pick whichever shape is more convenient for what you're about to do with the result.

editor.html
import numpy as np

arr = np.array([0, 3, 0, 7, 0, 2])
print(np.argwhere(arr))
localhost:3000

2Practical Example

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

editor.html
import numpy as np

matrix = np.array([[0, 5], [3, 0]])
print(np.argwhere(matrix > 0))
localhost:3000

3Best Practices

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

1. Use argwhere() when you want to iterate over matching elements one full coordinate tuple at a time

2. Use the single-argument np.where() instead when you specifically need separate per-axis index arrays, such as for direct fancy indexing

3. Combine argwhere() with a comparison to locate elements exceeding a specific condition

⚠️

Tip: np.argwhere(condition) and the single-argument form of np.where(condition) find the same matching positions, but structure the result differently — argwhere() groups each match's full coordinates into one row, while where() returns separate per-axis index arrays; pick whichever shape is more convenient for what you're about to do with the result.

editor.html
import numpy as np

arr = np.array([0, 3, 0, 7, 0, 2])
print(np.argwhere(arr))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([0, 3, 0, 7, 0, 2])
print(np.argwhere(arr))
Example 02Advanced Example
import numpy as np

matrix = np.array([[0, 5], [3, 0]])
print(np.argwhere(matrix > 0))

Best Practices

  • Use argwhere() when you want to iterate over matching elements one full coordinate tuple at a time
  • Use the single-argument np.where() instead when you specifically need separate per-axis index arrays, such as for direct fancy indexing
  • Combine argwhere() with a comparison to locate elements exceeding a specific condition

Interview Question

How does the shape of np.argwhere()'s result differ from np.where(condition) called with a single argument?

Hint: Think about grouping coordinates per element versus separating them per axis.

np.argwhere() returns a single 2D array where each row holds the complete set of coordinates for one matching element, so for a 2D input, each row is a (row, column) pair grouped together. np.where(condition), called with just one argument, instead returns a tuple containing separate 1D arrays, one per dimension — all the row indices in one array, all the column indices in another. Both describe exactly the same matching positions, just structured differently: argwhere() groups by element, where() groups by axis.

Exercises

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

arr = np.array([0, 3, 0, 7, 0, 2])
print(np.argwhere(arr))

Frequently Asked Questions

How does the shape of np.argwhere()'s result differ from np.where(condition) called with a single argument?

np.argwhere() returns a single 2D array where each row holds the complete set of coordinates for one matching element, so for a 2D input, each row is a (row, column) pair grouped together. np.where(condition), called with just one argument, instead returns a tuple containing separate 1D arrays, one per dimension — all the row indices in one array, all the column indices in another. Both describe exactly the same matching positions, just structured differently: argwhere() groups by element, where() groups by axis.

Related Functions

np-whereboolean-indexingnp-count-nonzero