Python Reference ยท Standard Library: Data Structures and Functional Tools
The array module
Reference for Python's array module: compact typed arrays, type codes, tobytes and frombytes, append and extend, overflow errors and when to use NumPy instead.
array.array stores numbers of one fixed C type in a compact block of memory. A list of a million integers holds a million pointers to separate objects; an array('i') holds just four bytes per value. It behaves like a list in most ways (indexing, slicing, append, extend, iteration) but only accepts values of its type.
Use it for large numeric sequences, for binary file I/O and for passing raw buffers around. For math on whole arrays, NumPy is the better tool.
from array import array a = array("i", [1, 2, 3]) a.append(4)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
array.array(typecode[, initializer])
| Name | Description |
|---|---|
typecode |
One character naming the C type, see the table below. |
initializer |
Optional list, bytes-like object, iterable, or (for 'w') a string. |
| Type code | C type | Typical size (bytes) |
|---|---|---|
'b', 'B' |
signed / unsigned char | 1 |
'h', 'H' |
signed / unsigned short | 2 |
'i', 'I' |
signed / unsigned int | 4 |
'l', 'L' |
signed / unsigned long | 4 or 8 |
'q', 'Q' |
signed / unsigned long long | 8 |
'f', 'd' |
float / double | 4 / 8 |
'w' |
Unicode character (3.13+) | 4 |
Common methods: append, extend, insert, pop, remove, index, count, reverse, tolist, tobytes, frombytes, tofile, fromfile, byteswap, buffer_info. Attributes: typecode, itemsize.
Return value: a new array. Raises: TypeError for a wrong value type or type code; OverflowError when a value does not fit the C type; ValueError for frombytes data whose length is not a multiple of itemsize, or an unknown type code.
Usage notes
- Fixed type, checked values. Integers outside the range of the chosen code raise
OverflowError; floats cannot go into an integer array. - Sizes depend on the platform for
'l'and'i'; checkitemsizeif you write binary files meant for other machines, or usestructwith explicit sizes. - Slicing returns another array, and
+and*work on arrays of the same type code. - Byte order of
tobytes()is the machine's native order; usebyteswap()when exchanging with big-endian data. - Reading and writing files:
a.tofile(f)anda.fromfile(f, n)work with real binary files opened in'rb'or'wb'mode. - Shares memory with a
memoryview, so writes through the view change the array, and the array cannot be resized while a view exists. - No vectorized math.
a * 2repeats the array rather than doubling each value. For numerical work use NumPy. - Lists are fine for small data;
arraypays off for large, homogeneous numeric data.