The array module provides the array.array type, a compact sequence that stores values of a single C numeric type such as int or float. Use it when you need to hold many numbers in much less memory than a list, or when you must read and write raw binary data. For numerical computing and vector maths, NumPy is usually the better choice.
A list stores references to full Python objects, while an array stores the raw machine values contiguously. That is where the memory saving comes from.
You create an array with a type code that fixes the element type:
Common type codes:
| Code | C type | Typical size in bytes | Python type |
|---|---|---|---|
b / B | signed / unsigned char | 1 | int |
h / H | signed / unsigned short | 2 | int |
i / I | signed / unsigned int | 2 or 4 (usually 4) | int |
l / L | signed / unsigned long | 4 or 8 | int |
q / Q | signed / unsigned long long | 8 | int |
f | float | 4 | float |
d | double | 8 | float |
The u type code for wide characters is deprecated in favour of w (Python 3.13+), and you should use str for text. Check ints.itemsize for the exact size on your platform.
Arrays behave much like lists for indexing, slicing, iteration and common methods:
The difference is strictness. Appending a value of the wrong type or out of range raises an error:
On a typical 64-bit CPython, the list object holds about 8 MB of pointers (and the integer objects add more on top), while the array holds about 4 MB. Exact numbers depend on the version and platform, so measure on your own system.
Arrays can be written to and read from files as raw bytes, which is handy for simple binary formats:
Other useful methods are tobytes(), frombytes() and byteswap() for converting endianness. Arrays also support the buffer protocol, so they can be wrapped with memoryview or passed to socket.send without copying.
| Feature | list | array.array | numpy.ndarray |
|---|---|---|---|
| Element types | Mixed | One numeric type | One dtype |
| Memory per element | Pointer plus object | Raw value | Raw value |
| Vectorised math | No | No | Yes |
| Multi-dimensional | Nested lists | No | Yes |
| Standard library | Yes | Yes | No |
Choose array when you want a compact, dependency-free, one-dimensional numeric container or raw binary I/O. Choose NumPy for arithmetic across many elements, and a list for general purpose data.
| Member | Purpose |
|---|---|
a.typecode | The type code character used to create the array |
a.itemsize | Size of one element in bytes |
a.buffer_info() | Tuple of memory address and length in elements |
a.reverse() | Reverse the items in place |
a.tobytes() / array.frombytes(b) | Convert to and from raw bytes |
a.byteswap() | Swap byte order of every item |
The byte output assumes a little-endian machine, which covers most current desktops and servers. When exchanging files between different machines, agree on a byte order and call byteswap() when needed.
Not necessarily. Arrays save memory, but each access still creates a Python int or float object, so loops are not much faster. For speed, use NumPy vector operations.
Not general strings. It holds numbers only (and single characters with the w type code on recent versions). Use a list of str or a bytearray for bytes.
Use a.tolist() for array to list and array("i", my_list) for the reverse.
array support 2D data?No. It is one-dimensional. Use nested arrays, or NumPy for real multi-dimensional support.
The array module gives you a typed, memory-efficient one-dimensional sequence with list-like methods and fast binary file I/O. Pick the type code carefully, remember that values are range-checked, and reach for NumPy when you need vectorised numerical work.