← Back to NumPy Course | Chapter 3: Array Attributes | Lesson 3 of 6

size

size is the total count of elements in the array, no matter how many dimensions it has.

In this page:

  1. size
Syntax
python
arr.size

size

size multiplies all the shape entries together. A (3, 4) array has size 12. It is different from len(), which only returns the length of the first axis.

Note: Use arr.size to check that a reshape target holds exactly the same number of elements.

Example: size

python
import numpy as np

m = np.zeros((3, 4))
print("size:", m.size)
print("len:", len(m))
print("shape product:", m.shape[0] * m.shape[1])

# Output:
# size: 12
# len: 3
# shape product: 12
Related Topics
Common Mistakes
  1. Using len() and expecting the total element count
  2. Reshaping to a shape whose product differs from size
  3. Calling size like a function
Chapter Summary
  • size is the total number of elements
  • It is the product of shape
  • len returns only the first axis
  • It is an attribute
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.