Python Practice › NumPy Programs › Program 65
Count non-zero elements
Write a NumPy program to count the non-zero elements.
Input
[[4, 8, 0], [2, 0, 6]]
Output
4
Approach
np.count_nonzero.
Solution
import numpy as npprint(np.count_nonzero([[4, 8, 0], [2, 0, 6]]))Output
4
Learn the concept
New to this topic? Read the lesson, then come back.