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.
import numpy as np
arr = np.array([1, 2, 3], dtype=np.int64)
print(arr.itemsize)2Practical Example
Here is a real-world application of ndarray.itemsize showing how it is used in production NumPy code.
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)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.
import numpy as np
arr = np.array([1, 2, 3], dtype=np.int64)
print(arr.itemsize)