An array
is a collection of homogenous elements stored at contagious locations in the memory . Python does not have direct standard data structure for accessing low-level arrays, the closest one is the list
type which is actually implemented through arrays. However, the list type is implemented in such a way that it supports heterogenous types.
In this article we will see how we can be able to use low-level arrays in Python.
Python offers the array
module which provide an array data structure that is more efficient than the standard list when it comes to low-level operations. The array objects strictly supports only homogenous low-level types, where the type is specified at instantiation.
Creating arrays
Arrays can be created using the array
class.
If you are familiar with arrays from other relatively low-level languages such as C and C++, you will find array.array()
very similar. The class provides an interface to low-level, strongly typed arrays.
The syntax is as follows:
array(typecode, iterable = None)
copy
typecode |
Required. A code specifying the type of data which is stored in the array. |
iterable |
An iterable (such as a list or tuple) containing values which are convertible to the typecode specified in the first parameter. |
The typecode
parameter is a single character used to represent a low-level C type that the items in the array will be of. The typecodes supported are as shown below:
Type Code | C Type | minimum size in bytes |
---|---|---|
b |
signed integer | 1 |
B |
unsighned integer | 1 |
u |
Unicode character | 2 |
h |
signed integer | 2 |
H |
unsigned integer | 2 |
i |
signed integer | 2 |
I |
unsigned integer | 2 |
l |
signed integer | 4 |
L |
unsigned integer | 4 |
q |
signed integer | 8 |
Q |
unsigned integer | 8 |
f |
floating point | 4 |
d |
floating point | 8 |
To create an array, we have to first import the array module or the array class directly in our program
In the above example, we created an array object from the elements of a tuple. If we do not provide the second argument, an empty array will be created.
Array Operations
The arrays shares a lot of functionality with Python lists including indexing, iteration, and deletion of elements. Just like lists, arrays, are mutable, allowing them to be manipulated in-place.
Indexing and assignment
Arrayscan be indexed with both negative and positive index notations.
We can also assign a value at a particular index using the index assignment notation.
Inserting values
We can populate the array
with values after instantiation using various methods, such as append(),
insert()
, and extend()
.
The append()
and insert()
methods are used to add a single element to the array. The append()
method adds an element at the end of the array, while insert() allows us to insert an element at an arbitrary position.
The extend()
method is used to add multiple elements to the array. It adds all the element of an iterable object at the end of the array. The iterable should contain elements of the supported type.
Removing elements
We can remove elements from the array using the remove()
and pop()
methods. The pop()
method, removes the element at the specified index, while the remove()
method removes the first occurrence of the element given as an argument.
The pop()
method removes and returns the element at the end of the array if called without any argument, otherwise, it removes and returns the element at the specified index.
array methods
The methods of array objects and their usage are as summarized below.
method | usage |
---|---|
append(v) |
Add an element at the end of the array. |
count(v) |
Returns the number of occurrences of element 'v' in the array. |
extend(iterable) |
Add the elements of the 'iterable' at the end of the array. |
index(v) |
Return the index of the first occurrence of element v in the array. |
insert(i, v) |
Insert element v at index i. |
pop(i =None) |
Remove and return the element at index i. If i is not given, removes the item at the last index of the array. |
remove(v) |
Remove the first occurrence of element v in the array. |
reverse() |
Reverse the elements of the array in-place. |
itemsize() |
Returns the length in bytes of one array item in the internal representation |
buffer_info() |
Returns a tuple (address, length) giving the current memory address and the length in elements of the buffer used to hold array’s contents. |
fromlist(L) |
This method appends items from the list argument to the end of the array |
fromunicode(S) |
This method extends array with data from a Unicode string. |
tolist() |
This method returns a list with elements of the array. |
tounicode() |
This method returns a string containing the array data represented as a sequence of Unicode characters. |