w3resource

NumPy: Find indices of elements equal to zero in a NumPy array

NumPy: Array Object Exercise-115 with Solution

Write a NumPy program to find indices of elements equal to zero in a NumPy array.

Sample Solution:

Python Code:

import numpy as np
nums = np.array([1,0,2,0,3,0,4,5,6,7,8])
print("Original array:")
print(nums)
print("Indices of elements equal to zero of the said array:")
result = np.where(nums == 0)[0]
print(result)

Sample Output:

Original array:
[1 0 2 0 3 0 4 5 6 7 8]
Indices of elements equal to zero of the said array:
[1 3 5]

Pictorial Presentation:

Python NumPy: Find indices of elements equal to zero in a NumPy array

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to create random set of rows from 2D array.
Next: Write a NumPy program to compute the histogram of a set of data.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Set comprehension:

>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}