Python array Module
Example
Create an integer array and work with it:
from array import array
a = array('i', [1, 2, 3])
a.append(4)
print(a)
print(a[0])
Try it Yourself »
Definition and Usage
The array
module provides compact arrays of basic values (like integers and floats).
Unlike lists, arrays store elements in a typed, tightly packed representation, which uses less memory and can be faster for large numeric data.
Members
Member | Description |
---|---|
append() | Append a new item to the end of the array. |
array() | Create a new array. typecode is a single character (e.g. 'b' , 'i' , 'f' ) that decides the item type. |
buffer_info() | Return a tuple (address, length) giving the current memory info. |
byteswap() | Swap the bytes of all items (endian conversion for multi-byte types). |
clear() | Remove all items from the array (empty it). |
count() | Return the number of occurrences of x . |
extend() | Append items from an iterable or another array. |
frombytes() | Append items from a bytes object, interpreted using the array's type. |
fromfile() | Read n items (as machine values) from a file object. |
fromlist() | Append items from a list after type checking. |
index() | Return the index of the first occurrence of x . |
insert() | Insert a new item x into the array at position i . |
itemsize | Size in bytes of one array item. |
pop() | Remove and return the item at index i (last item by default). |
remove() | Remove the first occurrence of x . |
reverse() | Reverse the order of the items in place. |
tobytes() | Convert the array to a bytes object containing the raw machine values. |
tofile() | Write all items (as machine values) to a file object. |
tolist() | Convert the array to a list of Python values. |
tounicode() | Convert the array with type code 'u' to a unicode string. |
typecode | The type code character used by the array. |
typecodes | String of all available type code characters on this build. |