Python Array Module
In this tutorial, we look at the array module present in Python. The array module is used for the efficient handling of numeric values. We look at similarities and differences between lists and arrays.
The Array module
Arrays are similar to lists in Python as they store an ordered collection of items. However, unlike lists, the type of objects stored in arrays is constrained. Consider the example given below.
l1 = ['cat', 23, 45.12] a1 = array.array('i', [1, 32, 837]) a2 = array.array('d', [23.1, 34.33, 123.35])
‘l1’ is a list. We see that it contains different types of objects (string, int, and float in this case).
a1 is an array. It contains objects of the same type, namely int.
a2 is an array. It contains objects of the same type, namely float.
The array module compactly represents such arrays. It helps us deal efficiently with numeric Python objects. The following type codes are defined.
To know more details about the array module, please read the official documentation.
Operations using Arrays with implementation
For all operations, we need to first import the array module. It is common practice to import it as arr.
# import the array module import array as arr
Creating Python Arrays
Unlike lists, we need to declare the array object while specifying the Python type.
# declaration of a Python array a = arr.array('d', [4.12, 323.1, 5]) print(a)
Accessing elements in a Python Array
Just as in lists, we can access elements in an array using their indices.
Python uses 0-based based indexing and allows valid negative indices.
print(a[1]) # prints element at index 1 print(a[-3]) # prints element at index -3 # i.e. 3rd element from the end of the array
Slicing elements in a Python Array
Just as in lists, we can access a valid range of elements in the array by using the slice (:) operator.
print(a[0:2]) # prints elements starting from index 0 (inclusive) to # index 2(not inclusive) print(a[1:]) # prints elements starting from index 1 (inclusive) till # the end of the array print(a[:]) # prints all elements from the start till the end of # the array print(a[::2]) # prints every second element from the start of the # array till the end of the array print(a[-1::-2]) # prints every second element from the end of the array # going backwards till the start of the array
array('d', [4.12, 323.1]) array('d', [323.1, 5.0]) array('d', [4.12, 323.1, 5.0]) array('d', [4.12, 5.0]) array('d', [5.0, 4.12])
Updating a Python Array
This is again, similar to the operations present for lists as Python Arrays are mutable.
We can update individual elements by accessing their indices.
We can update a range of elements with the help of slicing.
To add a single element to the end of the array, we can use the append() function.
We can insert a single element at an index of the array using the insert() function.
We can add multiple elements to the end of the array using the extend() function.
To insert multiple elements into the array at an index, we can use the slice assignment operation.
We can concatenate arrays using the ‘+’ operator.
# updating one element a[0] = 12 print(a) # updating elements in the range 0 to 2 a[:2] = arr.array('d', [21, 213]) print(a) # adding an element to the end of the array a.append(65.44) print(a) # inserting an element at the index 2 a.insert(2, 33.46) print(a) # extending the array a.extend([3993, 377, 200]) print(a) # inserting multiple elements at the index 4 a[4:4] = arr.array('d', [2, 123.66, 2322]) print(a) # concatenating arrays using '+' a = a + arr.array('d', [588, 30.22]) print(a)
array('d', [12.0, 323.1, 5.0]) array('d', [21.0, 213.0, 5.0]) array('d', [21.0, 213.0, 5.0, 65.44]) array('d', [21.0, 213.0, 33.46, 5.0, 65.44]) array('d', [21.0, 213.0, 33.46, 5.0, 65.44, 3993.0, 377.0, 200.0]) array('d', [21.0, 213.0, 33.46, 5.0, 2.0, 123.66, 2322.0, 65.44, 3993.0, 377.0, 200.0]) array('d', [21.0, 213.0, 33.46, 5.0, 2.0, 123.66, 2322.0, 65.44, 3993.0, 377.0, 200.0, 588.0, 30.22])
Searching in a Python Array
We can use the index() function to return the index of the first occurrence of a value in an array.
# displaying the index of the first # occurence of 5 in the array print(a.index(5))
Deletion in a Python Array
The del statement is used to delete an element in the array at a given index.
We can use the remove() function to delete the first occurrence of a value in an array.
We also have the pop() function to pop out the element at a given index.
We can also use the del statement to delete a range of elements with the help of the slice operator.
If we need to delete the array itself, that too can be done using del.
# deleting the element at index 1 del a[1] print(a) # deleting the first occurrence of 377 in the array a.remove(377) print(a) # popping out the value at index 4 and then printing the array print(a.pop(4)) print(a) # deleting the elements in the range 4 to 8 # and then deleting all the elements in the array del a[4:8] print(a) del a[:] print (a) # deleting the array del a # printing 'a' now will lead to an error
array('d', [21.0, 33.46, 5.0, 2.0, 123.66, 2322.0, 65.44, 3993.0, 377.0, 200.0, 588.0, 30.22]) array('d', [21.0, 33.46, 5.0, 2.0, 123.66, 2322.0, 65.44, 3993.0, 200.0, 588.0, 30.22]) 123.66 array('d', [21.0, 33.46, 5.0, 2.0, 2322.0, 65.44, 3993.0, 200.0, 588.0, 30.22]) array('d', [21.0, 33.46, 5.0, 2.0, 588.0, 30.22]) array('d')
Conclusion
In this tutorial, we looked at the Array module in Python. We saw how arrays are used to handle numeric values. However, arrays are rarely used when compared to lists as their only advantage is their efficiency in storage. Also, it is not easy to work with arrays for various mathematical operations. If we wish to do so, we should use the help of the NumPy library.
array — Efficient arrays of numeric values¶
This module defines an object type which can compactly represent an array of basic values: characters, integers, floating point numbers. Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained. The type is specified at object creation time by using a type code, which is a single character. The following type codes are defined:
- It can be 16 bits or 32 bits depending on the platform.
Changed in version 3.9: array(‘u’) now uses wchar_t as C type instead of deprecated Py_UNICODE . This change doesn’t affect its behavior because Py_UNICODE is alias of wchar_t since Python 3.3.
The actual representation of values is determined by the machine architecture (strictly speaking, by the C implementation). The actual size can be accessed through the array.itemsize attribute.
The module defines the following item:
A string with all available type codes.
The module defines the following type:
class array. array ( typecode [ , initializer ] ) ¶
A new array whose items are restricted by typecode, and initialized from the optional initializer value, which must be a list, a bytes-like object , or iterable over elements of the appropriate type.
If given a list or string, the initializer is passed to the new array’s fromlist() , frombytes() , or fromunicode() method (see below) to add initial items to the array. Otherwise, the iterable initializer is passed to the extend() method.
Array objects support the ordinary sequence operations of indexing, slicing, concatenation, and multiplication. When using slice assignment, the assigned value must be an array object with the same type code; in all other cases, TypeError is raised. Array objects also implement the buffer interface, and may be used wherever bytes-like objects are supported.
Raises an auditing event array.__new__ with arguments typecode , initializer .
The typecode character used to create the array.
The length in bytes of one array item in the internal representation.
Append a new item with value x to the end of the array.
Return a tuple (address, length) giving the current memory address and the length in elements of the buffer used to hold array’s contents. The size of the memory buffer in bytes can be computed as array.buffer_info()[1] * array.itemsize . This is occasionally useful when working with low-level (and inherently unsafe) I/O interfaces that require memory addresses, such as certain ioctl() operations. The returned numbers are valid as long as the array exists and no length-changing operations are applied to it.
When using array objects from code written in C or C++ (the only way to effectively make use of this information), it makes more sense to use the buffer interface supported by array objects. This method is maintained for backward compatibility and should be avoided in new code. The buffer interface is documented in Buffer Protocol .
“Byteswap” all items of the array. This is only supported for values which are 1, 2, 4, or 8 bytes in size; for other types of values, RuntimeError is raised. It is useful when reading data from a file written on a machine with a different byte order.
Return the number of occurrences of x in the array.
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. If iterable is not an array, it must be iterable and its elements must be the right type to be appended to the array.
Appends items from the string, interpreting the string as an array of machine values (as if it had been read from a file using the fromfile() method).
New in version 3.2: fromstring() is renamed to frombytes() for clarity.
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.
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.
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.
Return the smallest i such that i is the index of the first occurrence of x in the array. The optional arguments start and stop can be specified to search for x within a subsection of the array. Raise ValueError if x is not found.
Changed in version 3.10: Added optional start and stop parameters.
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.
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.
Remove the first occurrence of x from the array.
Reverse the order of the items in the array.
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.)
New in version 3.2: tostring() is renamed to tobytes() for clarity.
Write all items (as machine values) to the file object f.
Convert the array to an ordinary list with the same items.
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.
When an array object is printed or converted to a string, it is represented as array(typecode, initializer) . The initializer is omitted if the array is empty, otherwise it is a string if the typecode is ‘u’ , otherwise it is a list of numbers. The string is guaranteed to be able to be converted back to an array with the same type and value using eval() , so long as the array class has been imported using from array import array . Examples:
array('l') array('u', 'hello \u2641') array('l', [1, 2, 3, 4, 5]) array('d', [1.0, 2.0, 3.14])
Packing and unpacking of heterogeneous binary data.
Packing and unpacking of External Data Representation (XDR) data as used in some remote procedure call systems.
The NumPy package defines another array type.