MysteryGuy
MysteryGuy

Reputation: 1151

Count the number of elements in array in Python whatever dimension it is

I want to count easily the number of elements in a NumPy array, but I don't know a priori their dimensions. Is there a generic function that counts the number of elements in a numpy array whatever its dimension is?

Upvotes: 4

Views: 6744

Answers (1)

Sasha Tsukanov
Sasha Tsukanov

Reputation: 1125

numpy.ndarray.size returns a number of elements in the array

>>> x = np.zeros((3, 5, 2), dtype=np.complex128)
>>> x.size
30

Upvotes: 5

Related Questions