array – arrays of numeric data
This module implements a subset of the corresponding CPython module,
as described below. For more information, refer to the original
CPython documentation: array.
Supported format codes: b, B, h, H, i, I, l,
L, q, Q, f, d (the latter 2 depending on the
floating-point support).
Classes
- class array.array(typecode[, iterable])
Create array with elements of given type. Initial contents of the array are given by iterable. If it is not provided, an empty array is created.
In addition to the methods below, array objects also implement the buffer protocol. This means the contents of the entire array can be accessed as raw bytes via a
memoryviewor other interfaces which use this protocol.- append(val)
Append new element val to the end of array, growing it.
- extend(iterable)
Append new elements as contained in iterable to the end of array, growing it.
- __getitem__(index)
Indexed read of the array, called as
a[index](whereais anarray). Returns a value if index is anintand anarrayif index is a slice. Negative indices count from the end andIndexErroris thrown if the index is out of range.Note:
__getitem__cannot be called directly (a.__getitem__(index)fails) and is not present in__dict__, howevera[index]does work.
- __setitem__(index, value)
Indexed write into the array, called as
a[index] = value(whereais anarray).valueis a single value if index is anintand anarrayif index is a slice. Negative indices count from the end andIndexErroris thrown if the index is out of range.Note:
__setitem__cannot be called directly (a.__setitem__(index, value)fails) and is not present in__dict__, howevera[index] = valuedoes work.
- __len__()
Returns the number of items in the array, called as
len(a)(whereais anarray).Note:
__len__cannot be called directly (a.__len__()fails) and the method is not present in__dict__, howeverlen(a)does work.
- __add__(other)
Return a new
arraythat is the concatenation of the array with other, called asa + other(whereaand other are botharrays).Note:
__add__cannot be called directly (a.__add__(other)fails) and is not present in__dict__, howevera + otherdoes work.
- __iadd__(other)
Concatenates the array with other in-place, called as
a += other(whereaand other are botharrays). Equivalent toextend(other).Note:
__iadd__cannot be called directly (a.__iadd__(other)fails) and is not present in__dict__, howevera += otherdoes work.
- __repr__()
Returns the string representation of the array, called as
str(a)orrepr(a)`(whereais anarray). Returns the string"array(<type>, [<elements>])", where<type>is the type code letter for the array and<elements>is a comma separated list of the elements of the array.Note:
__repr__cannot be called directly (a.__repr__()fails) and is not present in__dict__, howeverstr(a)andrepr(a)both work.