array.typecode
The typecode character used to create the array.

In [1]:
from array import *
In [2]:
# How to get the type code of an array?
array_num = array('i', [1,3,5,7,9])
array_num.typecode
Out[2]:
'i'

array.itemsize
The length in bytes of one array item in the internal representation.

In [3]:
array_num = array('i', [1,3,5,7,9,10,15])
array_num.itemsize
Out[3]:
4

array.append(x)
Append a new item with value x to the end of the array.

In [4]:
# How to append a value to the end of an array?
array_num = array('i', [1,3,5,7,9,10,15])
array_num.append(100)
array_num
Out[4]:
array('i', [1, 3, 5, 7, 9, 10, 15, 100])

array.buffer_info()
Return a tuple (address, length) giving the current memory address and the length in elements of the buffer used
to hold array’s contents.

In [5]:
# How to get the memory address and number of elements in an array?
array_num = array('i', [1,3,5,7,9,10,15])
array_num.buffer_info()
Out[5]:
(81782832, 7)

array.byteswap()
“Byteswap” all items of the array. It is useful when reading data from a file written on a machine
with a different byte order. This is only supported for values which are 1, 2, 4, or 8 bytes in size.
For other types of values, RuntimeError is raised.

In [6]:
array_num = array('i', [1,3,5,7,9,10,15])
array_num.byteswap()

array.count(x)
Return the number of occurrences of x in the array.

In [7]:
# How to count the number of occurrences of an element in an array?
array_num = array('i', [1,3,5,7,9,10,15,10])
array_num.count(10)
Out[7]:
2

array.extend(iterable)
Append items from iterable to the end of the array. If iterable is another array, it must have exactly the same
type code; if not, TypeError will be raised.

In [8]:
# How to extend an array with values from a list?
from array import *
array_num = array('i', [1, 3, 5, 7, 9])
print("Original array: "+str(array_num))
array_num.extend(array_num)
print("Extended array: "+str(array_num))
Original array: array('i', [1, 3, 5, 7, 9])
Extended array: array('i', [1, 3, 5, 7, 9, 1, 3, 5, 7, 9])

array.frombytes(s)
Appends items from the string, interpreting the string as an array of machine values.

In [9]:
a1 = array('i')
as_bytes = a1.tobytes()
a1.frombytes(as_bytes)
print('a1:', a1)
a1: array('i')

array.fromfile(f, n)
Read n items (as machine values) from the file object f and append them to the end of the array.
If less than n items are available, EOFError is raised, but the items that were available are still inserted
into the array. f must be a real built-in file object; something else with a read() method won’t do.

In [10]:
# How to read and write arrays in a file?
f = open("array.bin", "wb")
array("i", [1, 2, 3, 4]).tofile(f)
f.close()

nums = array("i")
f = open("array.bin", "rb")
nums.fromfile(f, 4)
print(nums)
array('i', [1, 2, 3, 4])

array.fromlist(list)
Append items from the list. This is equivalent to for x in list: a.append(x) except that if there is a type error,
the array is unchanged.

In [11]:
# How to add values from a list to an array?
ints = array("i", [1, 2])
nums.fromlist([3, 4])
print(nums)
array('i', [1, 2, 3, 4, 3, 4])

array.fromunicode(s)
Extends this array with data from the given unicode string. The array must be a type 'u' array; otherwise
a ValueError is raised. Use array.frombytes(unicodestring.encode(enc)) to append Unicode data to an array of some
other type.

In [12]:
# How to append a unicode string to an array?
unicodes = array("u", u"abcdef")
unicodes.fromunicode(u"xyz")
print(unicodes)
array('u', 'abcdefxyz')

array.index(x)
Return the smallest i such that i is the index of the first occurrence of x in the array.

In [13]:
# How to find the index of the first occurrence of a value in an array?
nums = array("i", [1, 2, 1, 3, 20])
print(nums.index(1))
print(nums.index(2))
print(nums.index(20))
0
1
4

array.insert(i, x)
Insert a new item with value x in the array before position i. Negative values are treated as being relative
to the end of the array.

In [14]:
# How to insert a value into an array?
nums = array("i", [1, 2, 4])
nums.insert(2, 3)
print( nums)
array('i', [1, 2, 3, 4])

array.pop([i])
Removes the item with the index i from the array and returns it. The optional argument defaults to -1,
so that by default the last item is removed and returned.

In [15]:
# How to pop elements off of an array?
array_num = array('i', [1,3,5,7,9,10,15,10])
array_num.pop(4)
Out[15]:
9

array.remove(x)
Remove the first occurrence of x from the array.

In [16]:
# How to remove the first occurrence of an element?
array_num = array('i', [1,3,5,7,9,10,15,10])
array_num.pop(2)
Out[16]:
5

array.reverse()
Reverse the order of the items in the array.

In [17]:
# How to reverse the items in an array?
array_num = array('i', [1,3,5,7,9,10,15,10])
array_num.reverse()
array_num
Out[17]:
array('i', [10, 15, 10, 9, 7, 5, 3, 1])

array.tobytes()
Convert the array to an array of machine values and return the bytes representation (the same sequence of bytes
that would be written to a file by the tofile() method.)

In [18]:
import numpy as np
array_num = np.array([[0, 1], [2, 3]], dtype='<u2')
array_num.tobytes()
Out[18]:
b'\x00\x00\x01\x00\x02\x00\x03\x00'

array.tofile(f)
Write all items (as machine values) to the file object f.

In [19]:
# How to read and write arrays in a file?
import array
f = open("array.bin", "wb")
array.array("i", [1, 2, 3, 4]).tofile(f)
f.close()

ints = array.array("i")
f = open("array.bin", "rb")
ints.fromfile(f, 3)
print(ints)
array('i', [1, 2, 3])

array.tolist()
Convert the array to an ordinary list with the same items.

In [20]:
# How to convert an array into a list?
import numpy 
array_num = numpy.array([1, 2, 3, 4, 5]) 
print("Array to list = ", array_num.tolist())
Array to list =  [1, 2, 3, 4, 5]

array.tounicode()
Convert the array to a unicode string. The array must be a type 'u' array; otherwise a ValueError is raised.
Use array.tobytes().decode(enc) to obtain a unicode string from an array of some other type.

In [21]:
# How to convert a unicode array into a unicode string?
array_str = array.array('u','Hello')
array_str.tounicode()
Out[21]:
'Hello'