On this page
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
- Native byte order by default: Call
.byteswap()when exchanging rawarraybytes across big-endian vs. little-endian machines (or use struct for explicit wire formats). - 1D only and no vectorized math: For 2D matrices or fast linear algebra, use NumPy;
arrayis ideal for compact stdlib buffers, audio samples, and typed weight storage. - Fixed typecodes: Appending a value of the wrong type or out-of-range integer raises
TypeErrororOverflowError.
Classes & Objects
- array.array — Return a new array whose items are restricted by typecode, and initialized from the optional initializer value, which must be a list, string or iterable over elements of the appropriate type. (Methods:
.append(),.buffer_info(),.byteswap(),.clear(),.count(),.extend()) - array.ArrayType — Return a new array whose items are restricted by typecode, and initialized from the optional initializer value, which must be a list, string or iterable over elements of the appropriate type. (Methods:
.append(),.buffer_info(),.byteswap(),.clear(),.count(),.extend())
Constants & Attributes
array.typecodes(str) ='bBuwhHiIlLqQfd'
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/argparse
- tuts/python3/stdlib_module/ast
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/wave
- tuts/python3/tutorials/advanced_python_internals/foreign_function_interfaces
- tuts/python3/tutorials/popular_application_domains/data_science_and_machine_learning
Related notes
- tuts/python3/stdlib_module/argparse Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ast Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/array/array Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/array/ArrayType Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/calendar Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/collections Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/datetime Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/heapq Linked here; shared tags: #stdlib