🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

ndarray.size

AI & DATA SCIENCE // ndarray-size

ndarray.size is the total number of elements in the array, equal to the product of all the dimensions in its shape.

Syntax

arr.size

Deep Dive Course

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.

editor.html
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.size)
print(len(arr))
localhost:3000

2Practical Example

Here is a real-world application of ndarray.size showing how it is used in production NumPy code.

editor.html
import numpy as np

cube = np.zeros((2, 3, 4))
print(cube.size)
localhost:3000

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.

editor.html
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.size)
print(len(arr))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.size)
print(len(arr))
Example 02Advanced Example
import numpy as np

cube = np.zeros((2, 3, 4))
print(cube.size)

Best Practices

  • 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
  • Use .size, rather than manually multiplying .shape's values together, to get the total element count directly
  • Combine .size with .itemsize, or use .nbytes directly, to estimate an array's memory footprint before allocating something large

Interview Question

For a 2D array with shape (3, 4), why does len(arr) return 3 while arr.size returns 12?

Hint: Think about what each one is actually measuring.

len(arr) reports the length of the array's first axis only — for a shape of (3, 4), that's 3 rows. arr.size instead reports the total number of individual scalar elements across every dimension combined, which for a (3, 4) array is 3 times 4, or 12. This is exactly the same distinction as calling len() on a list of lists, which counts the outer list's length, versus manually counting every value nested inside every sublist.

Exercises

MediumPractice using ndarray.size in a real scenario.
View Solution
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.size)
print(len(arr))

Frequently Asked Questions

For a 2D array with shape (3, 4), why does len(arr) return 3 while arr.size returns 12?

len(arr) reports the length of the array's first axis only — for a shape of (3, 4), that's 3 rows. arr.size instead reports the total number of individual scalar elements across every dimension combined, which for a (3, 4) array is 3 times 4, or 12. This is exactly the same distinction as calling len() on a list of lists, which counts the outer list's length, versus manually counting every value nested inside every sublist.

Related Functions

ndarray-shapendarray-ndimlen()