Array is a very popular data structure in C/C++, Java etc. In these languages array is defined as a collection of more than one elements of similar data type. Python doesn't have any built-in equivalent of array. It's List as well as Tuple is a collection of elements but they may of different types.
Python's array module emulates C type array. The module defines 'array' class. Following constructor creates an array object:
array(typecode, initializer)
The typecode argument determines the type of array. Initializer should be a sequence with all elements of matching type.
Following statement creates an integer array object:
>>> import array
>>> arr = array.array('i', range(5))
>>> arr
array('i', [0, 1, 2, 3, 4])
>>> type(arr)
<class 'array.array'>
>>> array.typecodes
'bBuhHiIlLqQfd'
The array module defines typecodes attribute which returns a string. Each character in the string represents a type code indicating C type and equivalent Python type:
Type code | C Type | Python Type |
---|
'b' | signed char | int |
'B' | unsigned char | int |
'u' | Py_UNICODE | Unicode character |
'h' | signed short | int |
'H' | unsigned short | int |
'i' | signed int | int |
'I' | unsigned int | int |
'l' | signed long | int |
'L' | unsigned long | int |
'q' | signed long long | int |
'Q' | unsigned long long | int |
'f' | float | float |
'd' | double | float |
The initializer argument can be a byte like object. Following example builds an array from byte representation of string.
>>> arr1 = array.array('b', b'Hello')
>>> arr1
array('b', [72, 101, 108, 108, 111])
The array class defines following methods:
array.buffer_info()
This method returns a tuple (address, length) giving the current memory address and the length in elements of the buffer used to hold array’s contents.
>>> arr = array.array('i', [0, 1, 2, 3, 4])
>>> arr.buffer_info()
(2201141755144, 5)
count()
This method returns the number of occurrences of certain element in the array.
>>> arr = array.array('i', [0, 1, 2, 3, 4])
>>> arr.count(2)
1
extend()
This method appends items from iterable to the end of the array or iterable which must have exactly the same type code; if not, TypeError will be raised.
>>> arr = array.array('i', [0, 1, 2, 3, 4])
>>> arr1 = array.array('i',[10,20,30])
>>> arr.extend(arr1)
>>> arr
array('i', [0, 1, 2, 3, 4, 10, 20, 30])
fromfile()
This method reads n items (as machine values) from the file object and appends to an array.
In following example, we first open a file in binary write mode.
>>> file = open('test.txt','wb')
>>> file.write(b'Hello Python')
12
>>> file.close()
We now use this file to append its data to array object.
>>> a = array.array('i')
>>> file = open('test.txt','rb')
>>> a.fromfile(file,file.tell())
>>> a
array('i', [1819043144, 2035294319, 1852794996])
append()
This method appends a new item to the end of the array
fromlist()
This method appends items from the list to array. This is equivalent to for x in list: a.append(x)
>>> a = array.array('i')
>>> a.append(10)
>>> a
array('i', [10])
>>> num = [20,30,40,50]
>>> a.fromlist(num)
>>> a
array('i', [10, 20, 30, 40, 50])
insert()
Insert a new item in the array before specified position
>>> a = array.array('i', [10, 20, 30, 40, 50])
>>> a.insert(2,25)
>>> a
array('i', [10, 20, 25, 30, 40, 50])
pop()
This method returns item at given index after removing it from the array.
>>> a = array.array('i', [10, 20, 30, 40, 50])
>>> x = a.pop(2)
>>> x
30
>>> a
array('i', [10, 20, 40, 50])
remove()
This method removes first occurrence of given item from the array.
>>> a = array.array('i', [10, 20, 30, 40, 50])
>>> a.remove(30)
>>> a
array('i', [10, 20, 40, 50])
tofile()
This method write all items to the file object having write permission enabled.
>>> a = array.array('i', [10, 20, 30, 40, 50])
>>> file = open("test.txt","wb")
>>> a.tofile(file)
>>> file.close()
>>> file = open("test.txt","rb")
>>> file.read()
b'\n\x00\x00\x00\x14\x00\x00\x00\x1e\x00\x00\x00(\x00\x00\x002\x00\x00\x00'