🚀 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.itemsize

AI & DATA SCIENCE // ndarray-itemsize

ndarray.itemsize is the number of bytes each individual element of the array occupies in memory, determined entirely by its dtype.

Syntax

arr.itemsize

Deep Dive Course

Every dtype has a fixed, well-defined size — int64 and float64 each take 8 bytes per element, int32 and float32 each take 4, bool takes 1 — and itemsize simply reports that number for whatever dtype the array currently has. Since NumPy stores every element contiguously with the same size, itemsize multiplied by the total number of elements, .size, gives you the array's total memory footprint in bytes, which is exactly what the separate .nbytes attribute computes directly.

1Understanding ndarray.itemsize

Every dtype has a fixed, well-defined size — int64 and float64 each take 8 bytes per element, int32 and float32 each take 4, bool takes 1 — and itemsize simply reports that number for whatever dtype the array currently has. Since NumPy stores every element contiguously with the same size, itemsize multiplied by the total number of elements, .size, gives you the array's total memory footprint in bytes, which is exactly what the separate .nbytes attribute computes directly.

💡

Multiplying .itemsize by .size manually is equivalent to just reading .nbytes directly — use .nbytes when you actually want the array's total memory footprint, since it's more direct and less error-prone.

editor.html
import numpy as np

arr = np.array([1, 2, 3], dtype=np.int64)
print(arr.itemsize)
localhost:3000

2Practical Example

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

editor.html
import numpy as np

small = np.array([1, 2, 3], dtype=np.int8)
large = np.array([1, 2, 3], dtype=np.int64)
print(small.itemsize, large.itemsize)
print(small.nbytes, large.nbytes)
localhost:3000

3Best Practices

Follow these guidelines when working with ndarray.itemsize:

1. Choose a smaller dtype, like int8 or float32, deliberately when itemsize matters for memory-constrained applications, like large datasets or embedded systems

2. Use .nbytes directly instead of manually multiplying .itemsize by .size, since it expresses the same intent more directly

3. Check itemsize when interfacing with external binary formats or C libraries that expect a specific fixed element size

⚠️

Tip: Multiplying .itemsize by .size manually is equivalent to just reading .nbytes directly — use .nbytes when you actually want the array's total memory footprint, since it's more direct and less error-prone.

editor.html
import numpy as np

arr = np.array([1, 2, 3], dtype=np.int64)
print(arr.itemsize)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([1, 2, 3], dtype=np.int64)
print(arr.itemsize)
Example 02Advanced Example
import numpy as np

small = np.array([1, 2, 3], dtype=np.int8)
large = np.array([1, 2, 3], dtype=np.int64)
print(small.itemsize, large.itemsize)
print(small.nbytes, large.nbytes)

Best Practices

  • Choose a smaller dtype, like int8 or float32, deliberately when itemsize matters for memory-constrained applications, like large datasets or embedded systems
  • Use .nbytes directly instead of manually multiplying .itemsize by .size, since it expresses the same intent more directly
  • Check itemsize when interfacing with external binary formats or C libraries that expect a specific fixed element size

Interview Question

How would you calculate an array's total memory usage in bytes using .itemsize, and why is there also a separate .nbytes attribute?

Hint: Think about what information you'd need to combine to get the total, and whether NumPy already provides a shortcut.

The total memory used by an array's data equals its total element count, .size, multiplied by the size of each individual element, .itemsize — this is exactly what NumPy's own .nbytes attribute computes and returns directly, without you needing to look up and multiply the two values yourself. .itemsize and .size remain useful separately, though, for cases where you need the per-element size specifically, like when interfacing with a binary file format or a C library that expects data in a particular fixed layout.

Exercises

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

arr = np.array([1, 2, 3], dtype=np.int64)
print(arr.itemsize)

Frequently Asked Questions

How would you calculate an array's total memory usage in bytes using .itemsize, and why is there also a separate .nbytes attribute?

The total memory used by an array's data equals its total element count, .size, multiplied by the size of each individual element, .itemsize — this is exactly what NumPy's own .nbytes attribute computes and returns directly, without you needing to look up and multiply the two values yourself. .itemsize and .size remain useful separately, though, for cases where you need the per-element size specifically, like when interfacing with a binary file format or a C library that expects data in a particular fixed layout.

Related Functions

ndarray-nbytesndarray-dtypendarray-size