This is the documentation for the latest development branch of MicroPython and may refer to features that are not available in released versions.

If you are looking for the documentation for a specific release, use the drop-down menu on the left and select the desired version.

array – arrays of numeric data

This module implements a subset of the correspondingCPythonmodule,as described below. For more information, refer to the originalCPython documentation:array.

Supported format codes:b,B,h,H,i,I,l,L,q,Q,f,d (the latter 2 depending on thefloating-point support).

Classes

classarray.array(typecode[,iterable])

Create array with elements of given type. Initial contents of thearray are given byiterable. If it is not provided, an emptyarray is created.

In addition to the methods below, array objects also implement the bufferprotocol. This means the contents of the entire array can be accessed as rawbytes via amemoryview or other interfaces which use this protocol.

append(val)

Append new elementval to the end of array, growing it.

extend(iterable)

Append new elements as contained initerable to the end ofarray, growing it.

__getitem__(index)

Indexed read of the array, called asa[index] (wherea is anarray).Returns a value ifindex is anint and anarray ifindex is a slice.Negative indices count from the end andIndexError is thrown if the index isout of range.

Note:__getitem__ cannot be called directly (a.__getitem__(index) fails) andis not present in__dict__, howevera[index] does work.

__setitem__(index,value)

Indexed write into the array, called asa[index]=value (wherea is anarray).value is a single value ifindex is anint and anarray ifindex is a slice.Negative indices count from the end andIndexError is thrown if the index is out of range.

Note:__setitem__ cannot be called directly (a.__setitem__(index,value) fails) andis not present in__dict__, howevera[index]=value does work.

__len__()

Returns the number of items in the array, called aslen(a) (wherea is anarray).

Note:__len__ cannot be called directly (a.__len__() fails) and themethod is not present in__dict__, howeverlen(a) does work.

__add__(other)

Return a newarray that is the concatenation of the array withother, called asa+other (wherea andother are botharrays).

Note:__add__ cannot be called directly (a.__add__(other) fails) andis not present in__dict__, howevera+other does work.

__iadd__(other)

Concatenates the array withother in-place, called asa+=other (wherea andotherare botharrays). Equivalent toextend(other).

Note:__iadd__ cannot be called directly (a.__iadd__(other) fails) andis not present in__dict__, howevera+=other does work.

__repr__()

Returns the string representation of the array, called asstr(a) orrepr(a)`(wherea is anarray). Returns the string"array(<type>,[<elements>])",where<type> is the type code letter for the array and<elements> is a commaseparated list of the elements of the array.

Note:__repr__ cannot be called directly (a.__repr__() fails) andis not present in__dict__, howeverstr(a) andrepr(a) both work.