Python Array Module
The array module is an extremely useful module for creating and maintaining arrays. These arrays are similar to the arrays in the C language. This article explains how to create arrays and several other useful methods to make working with arrays easier. This is a Python built-in module and comes ready to use in the Python Standard Library.
Importing Python array Module
To use the array module, we need to first import it. To do this, we can use keyword import.
Example of importing the array module
import array print(dir(array))
Output
In the above code example, we imported the array module and returned the list of all useful objects in the module.
Type codes in Python
Before going to use the array module, we must first understand the different type codes that we can use.
List of type codes
Type code | C Type | Python Type | Minimum Size (Bytes) |
b | signed char | int | 1 |
B | unsigned char | int | 1 |
u | Py_UNICODE | Unicode Character | 2 |
h | signed short | int | 2 |
H | unsigned short | int | 2 |
i | signed int | int | 2 |
I | unsigned int | int | 2 |
l | signed long | int | 4 |
L | unsigned long | int | 4 |
q | signed long long | int | 8 |
Q | unsigned long long | int | 8 |
f | float | float | 4 |
d | double | float | 8 |
We can get a string representing all the type codes by using the attribute typecodes.
Example of using typecodes in Python
import array print(array.typecodes)
Output
Creating an Array in Python
The module array has the following class:
Syntax of the class array in Python
class array.array(typecode[, initializer])
Let us create an array using the above class.
Example of creating an array in Python
import array numbers = array.array('i', [1,3,5,7]) print(numbers)
Output
There are several attributes and methods that we can use on the array object that we created.
Attributes of Python array Class
1. typecode
This attribute returns the type code of the array.
Example of using typecode in Python
import array numbers = array.array('i', [1,3,5,7]) print(numbers.typecode)
Output
2. itemsize
The attribute returns the size of the array in bytes,
Example of using itemsize in Python
import array numbers = array.array('i', [1,3,5,7]) print(numbers.itemsize)
Output
Methods of Python array Class
1. array.append(x)
The method takes an object of the same type code of the array as an argument and inserts the passed object at the end of the array.
Example of using append() in Python
import array numbers = array.array('i', [1,3,5,7]) print(numbers) numbers.append(8) print(numbers)
Output
array(‘i’, [1, 3, 5, 7, 8])
2. array.buffer_info()
The method returns a tuple containing the current memory address and the length of the buffer used to retain the contents of the array in elements.
Example of using buffer_info() in Python
import array numbers = array.array('i', [1,3,5,7]) print(numbers.buffer_info())
Output
3. array.byteswap()
The method does the byte swap operation on every element of the array. It has no return value.
Example of byteswap() in Python
import array numbers = array.array('i', [1, 3, 5]) print(numbers) numbers.byteswap() print(numbers)
Output
array(‘i’, [16777216, 50331648, 83886080])
4. array.count(x)
The method counts the passed x in an array and returns an integer representing the number of times x occurs in the array.
Example of using count() in Python
import array numbers = array.array('i', [1,3,5,7, 1]) print(numbers.count(1))
Output
5. array.extend(iterable)
We need to pass an iterable of the same type code to the method. The method appends the passed iterable to the array.
Example of using extend() in Python
import array numbers = array.array('i', [1,3,5,7, 1]) print(numbers) numbers.extend([2, 4, 8, 10]) print(numbers)
Output
array(‘i’, [1, 3, 5, 7, 1, 2, 4, 8, 10])
6. array.frombytes(s)
The method takes a bytes object and appends the object at the end of the array.
Example of using frombytes() in Python
import array numbers = array.array('i', [1,3,5,7, 1, 2]) print(numbers) numbers.frombytes(b'\x08\x00\x00\x00') print(numbers)
Output
array(‘i’, [1, 3, 5, 7, 1, 2, 8])
7. array.fromfile(f, n)
The method takes a file descriptor of a file and an integer representing the number of values to read. It appends the passed number of values from the passed file to the array.
Example of using fromfile() in Python
f = open("some_numbers.zip", "wb") array.array("i", [32, 1, 37, 45]).tofile(f) f.close() numbers = array.array('i', [1,3,5,7, 1, 2]) f = open("some_numbers.zip", "rb") numbers.fromfile(f, 4) print(numbers)
Output
8. array.fromlist(list)
The method appends the elements in the passed list to the end of the array. This method is a bit similar to array.extend().
Example of using fromlist() in Python
import array numbers = array.array('i', [1,3,5,7, 1, 2]) print(numbers) numbers.fromlist([3,2]) print(numbers)
Output
array(‘i’, [1, 3, 5, 7, 1, 2, 3, 2])
9. array.fromunicode(s)
The method appends the passed Unicode string to the end of the array of the type code ‘u’.
Example of using fromunicode() in Python
import array string = array.array('u', "Python") print(string) string.fromunicode(u'Geeks') print(string)
Output
array(‘u’, ‘PythonGeeks’)
10. array.index(x)
The method returns the index of the passed element. If the passed element has occurred multiple times in the array, then the index of the first occurrence is returned.
Example of using index() in Python
import array numbers = array.array('i', [1, 3, 5, 7, 1, 2]) print(numbers.index(1))
Output
11. array.insert(i, x)
The method takes a same type code object x and an integer i. It inserts x at the ith position of the array. If negative integers are passed, then the x will be inserted at the i-1th position of the array.
Example of using insert() in Python
import array numbers = array.array('i', [1, 3, 5, 7, 1, 2]) numbers.insert(2, 12) print(numbers)
Output
12. array.pop([i])
The method removes and returns the element at the passed index position of the array. If no argument is passed, it removes and returns the last element of the array.
Example of using pop() in Python
import array numbers = array.array('i', [1, 3, 5, 7, 1, 2]) print(numbers.pop(2))
Output
13. array.remove(x)
The array removes the passed element from the array. It has no return value. If the passed element occurs multiple times in the array, then the first occurrence of the element will be removed.
Example of using remove() in Python
import array numbers = array.array('i', [1, 3, 5, 7, 1, 2]) print(numbers) numbers.remove(1) print(numbers)
Output
array(‘i’, [3, 5, 7, 1, 2])
14. array.reverse()
The order of elements is reversed by this method. It has no return value.
Example of using reverse() in Python
import array numbers = array.array('i', [1, 3, 5, 7, 1, 2]) print(numbers) numbers.reverse() print(numbers)
Output
array(‘i’, [2, 1, 7, 5, 3, 1])
15. array.tobytes()
The method converts the array into an array of bytes and returns a byte object.
Example of using tobytes() in Python
import array numbers = array.array('i', [1,3,5,7, 1, 2]) print(numbers) print(numbers.tobytes())
Output
b’\x01\x00\x00\x00\x03\x00\x00\x00\x05\x00\x00\x00\x07\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00′
16. array.tofile(f)
The method takes a file descriptor and adds the elements of the array to the file of the passed file descriptor. Previous data in the file will be replaced by the new elements.
Example of using tofile() in Python
import array f = open("more_numbers.zip", "wb") array.array("i", [24, 25, 27, 29]).tofile(f) f.close() numbers = array.array('i') f = open("more_numbers.zip", "rb") numbers.fromfile(f, 4) print(numbers)
Output
17. array.tolist()
The method converts the array into a normal Python list object. The method doesn’t modify any elements of the array it is converting.
Example of using tolist() in Python
import array numbers = array.array('i', [1,3,5,7, 1, 2]) print(numbers) print(numbers.tolist())
Output
[1, 3, 5, 7, 1, 2]
18. array.tounicode()
The method converts the array of type code ‘u’ into a Unicode string and returns a string object representing the converted Unicode string.
Example of using tounicode() in Python
import array string = array.array('u', "PythonGeeks") print(string) print(string.tounicode())
Output
PythonGeeks
Array vs List in Python
The main difference between array and list is that list can store multiples types of data whereas an array can store only a single type of data.
Example of difference between array and list in Python
import array numbers = array.array('i', [1, 3, 5, 7, 1, 2]) print(numbers) numbers2 = [2, "lemon", 3.4] print(numbers2)
Output
[2, ‘lemon’, 3.4]
Array Operations in Python
1. Joining Two arrays in python
We can join two different arrays and form a new single array by using the addition operator (+).
Example of joining two arrays in Python
import array numbers1 = array.array('i', [1, 3, 5]) numbers2 = array.array('i', [10, 11]) numbers3 = numbers1 + numbers2 print(numbers3)
Output
2. Multiplying an array in Python
We can multiply an array by using the multiplication operator (*) in Python.
Example of multiplying an array in Python
import array numbers1 = array.array('i', [1, 3, 5]) print(numbers1*2)
Output
3. Indexing an array in Python
To get the element at a certain position of an array, we can use the square brackets [].
Example of indexing an array
import array numbers1 = array.array('i', [1, 3, 5, 4, 8]) print(numbers1[2])
Output
4. Slicing an array in Python
To get a selected sequence of elements from an array, we can use the following slicing syntax.
Syntax
array[start: end: step]
Example of slicing an array in Python
import array numbers1 = array.array('i', [1, 3, 5, 4, 8, 12, 1, 7, 2, 6]) print(numbers1[1: 8: 2])
Output
Interview Questions on Python array Module
Q1. Create an empty array of type code float using the array module.
Ans 1. Complete code is as follows:
import array from array import array ans = array('f') print(ans.typecode)
Output
Q2. Create an array of the first five multiples of seven.
Ans 2. Complete code is as follows:
from array import array ans = array('i', [i*7 for i in range(1, 6)]) print(ans)
Output
Q3. Write a program to concatenate the given two arrays.
array1 = [1, 2, 3, 4] array2 = [5, 6, 7]
Ans 3. Complete code is as follows:
from array import array array1 = array('i', [1, 2, 3, 4]) array2 = array('i', [5, 6, 7]) array3 = array1 + array2 print(array3)
Output
Q4. Write a program to reverse the given array.
arr = [6, 5, 4, 3, 2, 1]
Ans 4. Complete code is as follows:
from array import array arr = array('i', [6, 5, 4, 3, 2, 1]) arr.reverse() print(arr)
Output
Q5. Write a program to insert the element 3.5 at the index 3 in the given array.
arr = [6.5, 5.5, 4.5, 2.5, 1.5]
Ans 5. Complete code is as follows:
from array import array arr = array('f', [6.5, 5.5, 4.5, 2.5, 1.5]) arr.insert(3, 3.5) print(arr)
Output
Conclusion
In this article, we learned how to use Python array module to create arrays. Learn about several attributes and methods present in this module. See the difference between arrays and lists in Python. Furthermore, if you have any queries, kindly leave them in the comments section.