Python Glossary Русский

all Python 3arrayterm

array

module

Compact arrays of same-typed numbers: values are stored as C types rather than objects.

Syntax

from array import array

Reach for it when there are many numbers and memory matters: array('i') takes a few bytes per element instead of a full int object in a list. Plain list is enough for everyday tasks, and third-party NumPy suits array arithmetic. The type is fixed by a type code at creation, and a value of the wrong type or out of range raises an error.

Examples

from array import array
a = array('i', [1, 2, 3])
a.append(4)
print(a, a.itemsize)  # → array('i', [1, 2, 3, 4]) 4
a.append(1.5)  # → TypeError — тип элементов задан кодом 'i'

See also

Open in the full glossarydocs.python.orgReport an inaccuracy