Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.nonzero

Returns the indices of non-zero elements as one index array per input dimension. It also works with boolean conditions to locate entries where a predicate is true.

How it works

np.nonzero evaluates an array-like input and returns a tuple containing one index array for each dimension. Each array holds coordinates of non-zero elements, ordered according to row-major, C-style traversal; boolean conditions can therefore be passed to locate elements satisfying a predicate.

Syntax

numpy.nonzero(a)

Parameters

NameTypeDescription
aarray_likeArray-like input whose values are tested for being non-zero.

Returns

A tuple of arrays containing the indices of non-zero elements, with one array for each dimension of the input.

Key points

  • Use np.nonzero to locate entries whose values are not zero.
  • The output is a tuple, with a separate index array corresponding to every input dimension.
  • Each output array stores coordinates along its associated dimension.
  • Results are generated in row-major, C-style order, giving predictable coordinate traversal.
  • Pass a boolean comparison to identify locations meeting a condition rather than only literal non-zero values.

Common mistakes

  • The result is organized by dimension, not as one coordinate row per element; use np.argwhere when row-wise coordinates are needed.
  • For extracting values, prefer boolean indexing over indexing with np.nonzero, especially when handling zero-dimensional arrays.

Example: Find non-zero elements and matching positions

import numpy as np

x = np.array([[0, 2, 0], [3, 0, 4]])
rows, columns = np.nonzero(x)
print(rows)     # [0 1 1]
print(columns)  # [1 0 2]

mask = x > 2
print(np.nonzero(mask))

The function returns one index array per dimension; applying it to a boolean condition finds positions where the condition is true.

When to use it

Locate populated entries

Sparse or masked data

Find values satisfying a comparison

Thresholding

Obtain dimension-separated coordinates

Multidimensional coordinate extraction

Interview questions

QWhat does np.nonzero return for a two-dimensional array?

It returns a tuple with one array for each input dimension. The arrays contain the coordinates of non-zero elements, and the coordinates follow row-major C-style order.

Follow-up: How would you obtain one coordinate row per matching element? Use np.argwhere, which groups the indices by element.

QHow can np.nonzero find elements satisfying a condition?

A comparison such as a > 3 produces a boolean array. Because False is treated as zero, np.nonzero(a > 3) returns the positions where the comparison is true.

Follow-up: How can you retrieve the matching values instead of their coordinates? Use boolean indexing, for example a[a > 3].

QWhat input does np.nonzero accept, and what does it identify?

Its input parameter a is array-like. The function tests the values and reports the indices of entries that are not zero.

Follow-up: What ordering should callers expect? The returned indices follow row-major, C-style order.

  • numpy.argwhere
  • numpy.nonzero
  • numpy.atleast_1d

Source: numpy.nonzero. Checked 2026-10-15.