array.array, array.array.append, array.array.tolist
class array.array(typecode[, initializer])
array.array.typecode
array.array.itemsize
array.array.append(value, /)
array.array.tolist()Create a typed array, append a value, and convert it to a list.
| Parameter | Description |
|---|---|
typecode | A single character such as 'B'. array.typecodes lists the codes available in this Python. |
initializer | Optional bytes, bytearray, Unicode string (for 'u' or 'w'), or iterable of values of the right type. |
value | A number that must fit the typecode. Out-of-range values raise OverflowError. |
Returns: The constructor returns an array. typecode is a str. itemsize is an int. tolist returns a list. append returns None.
Raises
OverflowError— Raised when a value does not fit the type, such as 256 for 'B' or 128 for 'b' on Python 3.13.12.TypeError / ValueError— Raised for a bad typecode, a value of the wrong type, or an initializer the constructor does not accept.
- Do not treat itemsize of 'i' or 'l' as a portable constant. This example only asserts itemsize for 'B'.
import array
b = array.array("B", [0, 255])
print(b.typecode, b.itemsize, b.tolist())
try:
array.array("B", [256])
except OverflowError:
print("OverflowError")
try:
array.array("b", [128])
except OverflowError:
print("OverflowError")
b.append(1)
print(b.tolist())Expected output
B 1 [0, 255]
OverflowError
OverflowError
[0, 255, 1]