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

itemsize

itemsize says how many bytes a single element takes up.

In this page:

  1. itemsize
Syntax
python
arr.itemsize

itemsize

itemsize is the number of bytes per element, determined by dtype. int64 and float64 use 8 bytes, int32 and float32 use 4, and int8 uses 1. Choosing a smaller dtype directly reduces memory.

Note: Bool arrays use 1 byte per element, not 1 bit.

Example: itemsize

python
import numpy as np

for dt in (np.int8, np.int32, np.int64, np.float32, np.float64):
    print(np.dtype(dt).name, np.zeros(1, dtype=dt).itemsize)

# Output:
# int8 1
# int32 4
# int64 8
# float32 4
# float64 8
Related Topics
Common Mistakes
  1. Confusing bytes with bits
  2. Assuming all ints are 4 bytes
  3. Ignoring itemsize when memory is tight
Chapter Summary
  • itemsize is bytes per element
  • It depends on dtype
  • int64 and float64 are 8 bytes
  • Smaller dtypes save memory
🔒

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.