Python array Module for Compact C-Style Data

The Python array module provides an efficient, space-saving alternative to standard Python lists when handling large sequences of numeric data. By strictly requiring all elements to share a single C-style primitive type, it eliminates the per-element object overhead inherent to standard Python objects. This article explains what the array module provides, how its fixed-type system works, its primary memory advantages, and practical examples of utilizing C-style data types in Python.

Core Capabilities of the array Module

The module defines a single object type: array.array(typecode[, initializer]). Unlike standard Python lists, which store references to arbitrary, dynamically allocated Python objects, an array stores raw, unboxed machine values in contiguous memory blocks.

Key features provided by the module include:

Supported Type Codes

The type of data stored inside an array is defined using single-character type codes that correspond directly to C primitives:

Type Code C Type Minimum Size (Bytes) Python Type
'b' / 'B' signed char / unsigned char 1 int
'h' / 'H' signed short / unsigned short 2 int
'i' / 'I' signed int / unsigned int 2 int
'l' / 'L' signed long / unsigned long 4 int
'q' / 'Q' signed long long / unsigned long long 8 int
'f' float 4 float
'd' double 8 float

Attempting to insert a value outside the range or type dictated by the code raises a TypeError or OverflowError.

Memory Efficiency: array vs. Standard list

In a standard Python list containing integers, each element is a full Python object (typically 28 bytes on 64-bit platforms), plus an 8-byte pointer inside the list structure. Storing 10 million integers in a standard list requires over 300 megabytes of RAM.

In contrast, an array with the type code 'i' (4-byte signed integer) or 'h' (2-byte signed integer) allocates only the raw byte width per element in contiguous memory. Storing 10 million 4-byte integers in an array requires roughly 40 megabytes—reducing memory consumption by more than 85%.

Basic Usage and Binary Operations

Creating and populating an array mirrors standard Python sequence conventions:

import array

# Create an array of unsigned 16-bit integers ('H')
numbers = array.array('H', [100, 200, 300, 400, 500])

# Append an element
numbers.append(600)

# Check item size in bytes
print(numbers.itemsize)  # Output: 2

High-Performance Binary Serialization

Because arrays map directly to C-style memory buffers, they provide rapid serialization methods:

# Export directly to raw bytes
raw_bytes = numbers.tobytes()

# Reconstruct a new array from raw bytes
restored = array.array('H')
restored.frombytes(raw_bytes)

# Direct binary file write and read
with open('data.bin', 'wb') as f:
    numbers.tofile(f)

new_numbers = array.array('H')
with open('data.bin', 'rb') as f:
    new_numbers.fromfile(f, len(numbers))

When to Use the array Module

The array module is built into Python's standard library, requiring no external dependencies. It is ideal for: