In this tutorial, we will discuss, Python arrays and how they are different from the list. We will also cover some examples of Python arrays.
Arrays in General
An array is a collection of homogeneous (same) data types, and it stores all of its element in a contagious memory location.
Python Array Module
In python, we do not have standard support for Array data structure, instead of in python we often use the list data types to refer an array. But there is a problem with lists, its violets the basic property of an array which is a homogeneous collection of elements.
Though there is no in-built support for arrays in python, we can use a module name array to create an array data structure.
Syntax to create an array in Python using 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 in our program, then using that 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 decide the data type of all the elements and all the element 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 list as arrays, but theoretically, arrays cannot store different data types at once. And if we compare each data structure performance Arrays are faster than List because in array interpreter already has the knowledge of each element data type but in the list, the interpreter has to do work extra to check each element data type.
|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, array uses indexing too, with sq. 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 list 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])
Add Elements to the Array:
We can either use the append or extend method to add new elements in 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 Element from an Array:
To delete an element from an array we have 1 keyword del and 2 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 can we use an array?
We do not use 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 concepts and data science, numpy library also offers array data structure and its array support mathematical operation on matrix. So, we use arrays when we want to perform matrix operations using numeric values.
Here we are listing the complete tutorials: