Python Practice › NumPy Programs › Program 65

Count non-zero elements

EasyNumPy Programs · 65 of 100

Write a NumPy program to count the non-zero elements.

Advertisement
Input
[[4, 8, 0], [2, 0, 6]]
Output
4

Approach

  1. np.count_nonzero.

Solution

Python
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.

More numpy programs

← All 100 NumPy Programs

Leave a Comment