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.
import numpy as np
arr = np.array([0, 3, 0, 7, 0, 2])
print(np.argwhere(arr))2Practical Example
Here is a real-world application of np.argwhere() showing how it is used in production NumPy code.
import numpy as np
matrix = np.array([[0, 5], [3, 0]])
print(np.argwhere(matrix > 0))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.
import numpy as np
arr = np.array([0, 3, 0, 7, 0, 2])
print(np.argwhere(arr))