On this page
  1. array
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Zero-Copy Binary Buffer View (memoryview + tobytes)
  7. Gotchas & Boundaries
  8. Classes & Objects
  9. Constants & Attributes

array

Prev: argparse | Up: stdlib_module | Next: ast
Rel: datetime, zoneinfo, calendar, collections, heapq
Ref: docs.python.org — array
Tags: #public #python3 #stdlib


Import

import array

Overview

Compact, typed C-layout 1D numeric arrays (array.array) storing homogeneous integers or floats ('b', 'B', 'h', 'i', 'q', 'f', 'd') in contiguous memory instead of boxed Python objects. Uses 4–8× less RAM than a list[float] and supports zero-copy memoryview and binary file/buffer interchange (frombytes, tobytes).

Quick Start

from array import array

floats = array("d", [1.5, 2.5, 3.5])  # 64-bit C doubles (24 bytes total)
assert floats.itemsize == 8
assert sum(floats) == 7.5
assert array("d", floats.tobytes()) == floats

Implementation Walkthrough

1. Zero-Copy Binary Buffer View (memoryview + tobytes)

from array import array

samples = array("i", [10, 20, 30, 40])
mv = memoryview(samples)
assert mv.nbytes == len(samples) * samples.itemsize
raw = samples.tobytes()
restored = array("i")
restored.frombytes(raw)
assert restored == samples

Gotchas & Boundaries

Classes & Objects

Constants & Attributes

Linked from

Related notes