For an array with shape (3, 4), size is 3 times 4, or 12 — it counts every individual element across all dimensions, unlike len(arr), which only returns the length of the first, outermost, dimension. This distinction matters for multi-dimensional arrays: len() on a 2D array gives the number of rows, while .size gives the total number of individual values across the whole array.
1Understanding ndarray.size
For an array with shape (3, 4), size is 3 times 4, or 12 — it counts every individual element across all dimensions, unlike len(arr), which only returns the length of the first, outermost, dimension. This distinction matters for multi-dimensional arrays: len() on a 2D array gives the number of rows, while .size gives the total number of individual values across the whole array.
Don't confuse len(arr) with arr.size for multi-dimensional arrays — len() only measures the first axis, e.g. the number of rows, while .size is the total element count across every axis combined.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.size)
print(len(arr))2Practical Example
Here is a real-world application of ndarray.size showing how it is used in production NumPy code.
import numpy as np
cube = np.zeros((2, 3, 4))
print(cube.size)3Best Practices
Follow these guidelines when working with ndarray.size:
1. Use .size when you need the total element count across all dimensions, and len() only when you specifically want the length of just the first axis
2. Use .size, rather than manually multiplying .shape's values together, to get the total element count directly
3. Combine .size with .itemsize, or use .nbytes directly, to estimate an array's memory footprint before allocating something large
Tip: Don't confuse len(arr) with arr.size for multi-dimensional arrays — len() only measures the first axis, e.g. the number of rows, while .size is the total element count across every axis combined.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.size)
print(len(arr))