In this tutorial, we will discuss Python arrays and how they are different from the Python list. We will also cover some examples of Python arrays.
Arrays in General
Arrays in Python
The Python Array Module
In Python, we do not have the standard support for the array data structure. Instead, in Python, we often use the list data type, but there is a problem with the list data type. It violates the basic property of an array, which is to store homogeneous elements. Though there is no in-built support for arrays in Python, we can use a module named array to create an array data structure.
Syntax to Create an Array in Python Using the array Module:
import array variable_name = array.array('Type code',[item1 , item2, item3,])
import array arr = array.array('d', [1,2,3,4])
Behind the Code:
In the above code, we imported the array module into our program. Next, using the array module, we create an array containing elements [1,2,3,4]. In the above example, we have given ‘d’ as a type code that represents the data type of the elements.
Array Type codes in Python:
When we create an array, we need to pass a Unicode and a list of elements to the array () method. The Unicode decides the data type of all the elements, and the data type should match the type code.
array.array(‘Type code or UniCode’ , [list of items] )
|Type code||Python Type||Minimum size in bytes|
With the array module, we can only create an array of numeric elements.
Python List vs Python Array
In general, we use Python lists as arrays, but theoretically, arrays cannot store different data types at once. Also, if we compare the two data types on the basis of performance, arrays are faster than lists. This is so because, in arrays, the interpreter already has the knowledge of the data type of each element, but in the list, the interpreter has to do extra work to check the data type of each element.
|Python List||Array Module in Python|
python_list = [1,2,3,4,5,"This is a List"] print(python_list)
[1, 2, 3, 4, 5, 'This is a List']
import array python_array = array.array('i' , [1,2,4,5,6,7,]) print(python_array)
array('i', [1, 2, 4, 5, 6, 7])
Access Array Elements
Like a list, an array uses indexing too, with square brackets [ ].
import array arr = array.array ('i' , [1,3,4,6,7,10,11]) print("Element at Index 0:", arr) print("Element at Index 1:", arr) print("Element at Index 2:", arr)
Element at Index 0: 1 Element at Index 1: 3 Element at Index 2: 4
Python Array Slicing
Like lists, we can access a sequence of elements in arrays using slicing.
import array my_arr = array.array('i', [1,2,3,4,5,6,7,8,9,10]) print("Print First 5 elements of the array: ",my_arr[0:5]) print("Print all elements from index 2 to 6: ",my_arr[2:6]) print("Print all the all elements of the array: ",my_arr[ : ])
Print First 5 elements of the array: array('i', [1, 2, 3, 4, 5]) Print all elements from index 2 to 6: array('i', [3, 4, 5, 6]) Print all the all elements of the array: array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Adding Elements to the Array
We can either use the append or extend method to add new elements to the array.
import array my_arr = array.array('i', [1,2,3,4,5,6,7,8,9,10]) my_arr.append(100) my_arr.extend([200,300,400]) print(my_arr)
array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 200, 300, 400])
Delete Elements from an Array
To delete an element from an array, we have one keyword – del – and two methods, remove(value) and pop(index).
import array my_arr = array.array('i', [1,2,3,4,5,6,7,8,9,10]) my_arr.pop(0) # delete the element at 0 index my_arr.remove(10) # delete the value 10 del my_arr # delete the my_arr value print(my_arr)
array('i', [2, 3, 5, 6, 7, 8, 9])
When to Use an Array?
We do not use the Python array module to build arrays because we cannot perform a mathematical or arithmetic operation on array elements. In Python, we have another module known as NumPy, which is a famous python library used for mathematical computation and data science.
The numpy library also offers a special type of arrays called NumPy arrays that support mathematical operations on matrices. So, we use arrays when we want to perform matrix operations using numeric values.
That sums up this article on Python arrays. Although Python doesn’t offer support for arrays intrinsically, it comes with an array module to add arrays. Alternatively, you can use NumPy arrays as a suitable alternative.
Looking to learn Python? Check out TGB’s collection of Python tutorials: