Python Arrays: How to Create, Add, and Delete Elements?

Kate

Administrator
Команда форума

Table of Content​

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.
Python-Array.jpg


Arrays in General

We have a very popular data structure known as an array in most high-level programming languages, such as Java, C++, JavaScript , and C. An array is a collection of homogeneous elements, i.e., data of the same data type, and it stores all of its elements in a contiguous memory location.


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,])
Example:

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. Syntax:


array.array(‘Type code or UniCode’ , [list of items] )
Type codePython TypeMinimum size in bytes
'b'int1
'B'int1
'u'Unicode character2
'h'int2
'H'int2
'i'int2
'I'int2
'l'int4
'L'int4
'q'int8
'Q'int8
'f'float4
'd'float8
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 ListArray Module in Python
Example
python_list = [1,2,3,4,5,"This is a List"]
print(python_list)
Output:[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)

Output:array('i', [1, 2, 4, 5, 6, 7])

Access Array Elements

Like a list, an array uses indexing too, with square brackets [ ]. Example:

import array
arr = array.array ('i' , [1,3,4,6,7,10,11])
print("Element at Index 0:", arr[0])
print("Element at Index 1:", arr[1])
print("Element at Index 2:", arr[2])
Output:

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. Example:


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[ : ])
Output:

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. Example:

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)
Output:

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) . Example:


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[2] # delete the my_arr[2] value
print(my_arr)
Output:

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.

Conclusion​

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.

 
Сверху