NumPy
Python
arrays
indices
zero elements

Find indices of elements equal to zero in a NumPy array

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Finding zero values in a NumPy array is a small operation that appears in many real tasks, including mask construction, sparse-data cleanup, and debugging numerical pipelines. The main question is not whether NumPy can do it, but which indexing function returns the shape of result you actually need.

Using np.where for Basic Index Lookup

np.where returns index positions where a condition is true. For a one-dimensional array, that means a tuple containing one NumPy array of matching indices.

python
1import numpy as np
2
3values = np.array([4, 0, 7, 0, 9, 0])
4zero_positions = np.where(values == 0)[0]
5
6print(zero_positions)
text
[1 3 5]

The expression values == 0 creates a boolean mask. np.where turns that mask into index positions. The final [0] is necessary because NumPy returns one index array per dimension.

This is usually the most direct choice when you need positions for slicing, replacing, or reporting.

Working with Multidimensional Arrays

For two or more dimensions, np.where returns one array per axis. That is useful when you want row indices and column indices separately.

python
1import numpy as np
2
3matrix = np.array([
4    [5, 0, 1],
5    [0, 3, 0],
6    [8, 9, 2],
7])
8
9rows, cols = np.where(matrix == 0)
10
11print("rows:", rows)
12print("cols:", cols)
text
rows: [0 1 1]
cols: [1 0 2]

If you want coordinate pairs instead of separate arrays, combine them with zip or use np.argwhere.

python
coordinates = list(zip(rows, cols))
print(coordinates)
text
[(0, 1), (1, 0), (1, 2)]

This form is easier to read when iterating over matching cells.

Using np.argwhere for Coordinate Pairs

np.argwhere returns one row per match, where each row contains the full index coordinate. That makes it convenient for inspection and loops.

python
1import numpy as np
2
3matrix = np.array([
4    [0, 2],
5    [3, 0],
6    [0, 4],
7])
8
9zero_coords = np.argwhere(matrix == 0)
10print(zero_coords)
text
[[0 0]
 [1 1]
 [2 0]]

Compared with np.where, this structure is often easier to pass through logging code or convert into Python tuples.

python
coord_list = [tuple(coord) for coord in zero_coords]
print(coord_list)
text
[(0, 0), (1, 1), (2, 0)]

Boolean Masks and np.flatnonzero

Sometimes you do not need multidimensional coordinates at all. You only need the positions from the flattened view of the array. In that case, np.flatnonzero is compact and fast to read.

python
1import numpy as np
2
3matrix = np.array([
4    [1, 0, 3],
5    [0, 5, 0],
6])
7
8flat_positions = np.flatnonzero(matrix == 0)
9print(flat_positions)
text
[1 3 5]

This is useful when a downstream function works with flattened arrays, but it is a poor fit if you later need row and column locations.

Choosing the Right Result Shape

The correct function depends on how the indices will be used.

  • Use np.where(array == 0)[0] for one-dimensional arrays.
  • Use rows, cols = np.where(array == 0) when axis-specific indices matter.
  • Use np.argwhere(array == 0) when coordinate pairs are easier to handle.
  • Use np.flatnonzero(array == 0) when flattening is already part of the workflow.

The core operation is always the same: create a boolean mask and convert it into indices. Most mistakes come from choosing a return format that does not match the next step in the pipeline.

Common Pitfalls

  • Forgetting that np.where returns a tuple. In a one-dimensional case, use [0] to extract the actual index array.
  • Comparing floating-point results to exact zero after arithmetic. Use np.isclose when rounding error is possible.
  • Using flattened positions when you really need row and column coordinates. np.flatnonzero loses dimension structure.
  • Converting large result arrays to Python lists too early. Keep them as NumPy arrays while doing array operations.
  • Expecting argwhere and where to return the same shape. They represent the same matches in different formats.

Summary

  • NumPy finds zero indices by turning array == 0 into a boolean mask.
  • 'np.where is the standard tool for direct index lookup.'
  • 'np.argwhere is clearer when you want full coordinates for each match.'
  • 'np.flatnonzero is useful only when flattened indexing is acceptable.'
  • Matching the output format to the next processing step avoids unnecessary conversions.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.