Python:NumPy .nonzero()
Anonymous contributor
Published Nov 15, 2025
Contribute to Docs
The .nonzero() method returns the indices of elements in a NumPy array that are non-zero or evaluate to True. It is often used to locate positions of meaningful or valid data within an array.
Syntax
ndarray.nonzero()
Parameters:
This method takes no parameters.
Return value:
Returns a tuple of arrays, one for each dimension, containing the indices of elements that are non-zero.
Example: Finding Non-Zero Elements in a 1D Array
In this example, the indices of all non-zero elements in a 1D NumPy array are returned:
import numpy as nparr = np.array([0, 2, 0, 4, 5])result = arr.nonzero()print(result)
The output of this code is:
(array([1, 3, 4]),)
Codebyte Example
In this example, the positions of non-zero elements in a 2D array are identified:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours