A data structure or a special variable that contains the same types of data value throughout is known as an array. In Python, you can create an array using the Array module. Learn the operations used on Python array here.
Creating Python Array
A Python array consists of two parts. The first is the element that refers to the item stored in the array and the second is the variable in which the array is stored. Know that we can access and identify different elements in an array through indexing.
Process of Creating an Array
from array import * name_array = array(datatype, [Intializers]) This example demonstrates the process of creating an array containing integers. #creating an array from array import * arr = array(i, [5,6,7,8,9])
Common Type Codes
|Data Type||Value||Minimum Bytes|
|h||Signed short int||2|
|H||Unsigned short int||2|
|l||Signed long int||4|
|L||Unsigned long int||4|
Accessing Python Array Elements
Indexing is done to obtain any element from an array via the index. Index of an array or list starts from 0. We use the index operator  to access or obtain a particular element for an array.
#create two arrays and print specific elements from the arrays created from array import * array1 = array(i, [3,6,9,2,5]) print(“The third element is”, array1) array2 = array(d, [6.4, 3.2, 7.8, 1.2]) print(“The first element is”, array2)
The third element is 9 The first element is 6.4
Slicing Python Arrays
Slicing means taking out a particular section from an array. Indexing is required to carry out the slicing operation. We can start indexing either from the left side or the right side. To slice a Python array, we use the slicing operator (:), colon. For obtaining a section from the beginning of the Python array, we use [: index]. To slice a section starting for the end of the Python array, we can use [:-index].
Similarly, for a general section, we can use the start index before the colon and the end index after the colon to get a section containing all the elements for the start index to the end index. Note that the end index is not included in the sliced section of the Python array.
To reverse the whole array, we use [::-1], where the third part refers to the gap after each element starting from the start index. If nothing is mentioned then the operation starts from the beginning of the array. Look at the array presented below.
array = [2, 4, 6, 8, 10, 12, 14, 16] array[3:6] = [8, 10, 12] array[:7] = [2, 4, 6, 8, 10, 12, 14] array[2:] = [6, 8, 10, 12, 14, 16]
Changing and Adding Elements
Python array can be changed, updated, and amended to contain an entirely different set of values. They are known as mutable which means that the elements within can be changed. Operations can be performed upon an array to change it. For adding any element in an array, either insert() or append() function can be used to carry out the operation.
The difference between the two is that insert() can be used to add an element anywhere in the array whereas append() is used to add an element to the end of the array. Another function that goes by the name extend() can be used to add multiple values at the end.
from array import * arr_num = array(i, [3, 6, 2, 9, 4, 8, 1, 5, 7, 11]) print(arr_num) arr_num.insert(0, 0) print(arr_num) arr_num.append(22) print(arr_num) arr_num.extend(22, 31, 44) print(arr_num)
array(i, [3, 6, 2, 9, 4, 8, 1, 5, 7, 11]) array(i, [0, 3, 6, 2, 9, 4, 8, 1, 5, 7, 11]) array(i, [3, 6, 2, 9, 4, 8, 1, 5, 7, 11, 22]) array(i, [3, 6, 2, 9, 4, 8, 1, 5, 7, 11, 22, 31, 44])
An element or a section of the array could be changed in the same way by simply assigning a different value to it.
from array import * arr_num = array(i, [3, 6, 2, 9, 4, 8, 1, 5, 7, 11]) print(arr_num) arr_num = 15 print(arr_num)
array(i, [3, 6, 2, 9, 4, 8, 1, 5, 7, 11]) array(i, [3, 6, 2, 15, 4, 8, 1, 5, 7, 11])
Removing Python Array Elements
Removing an element from a Python array is possible because it is mutable, that is, it can be modified and changed according to the will of the user or programmer. The built-in functions used for this operation are remove() and pop(). The element to be removed should be present in the array. Del keyword is also used to delete an element or multiple elements at once from the given array.
from array import * arr_num = array(i, [3, 6, 2, 9, 4, 8, 1, 5, 7, 11]) print(arr_num) del arr_num print(arr_num) arr_num.remove(6) print(arr_num) arr_num.pop(4) print(arr_num)
array(i, [3, 6, 2, 9, 4, 8, 1, 5, 7, 11]) array(i, [3, 6, 2, 9, 8, 1, 5, 7, 11]) array(i, [3, 2, 9, 4, 8, 1, 5, 7, 11]) array(i, [3, 6, 2, 9, 8, 1, 5, 7, 11])
Python Lists Vs Arrays
A list in Python contains elements of different data types. On the other hand, an array in Python contains elements of the same data type.
Example of a list in Python.
list = [“hello”, 6, 77.9]
A list can contain numerical values, characters, another list, and other data types in the same data structure. It is ordered and both positive and negative indexing is allowed to access the elements for the list. A list consumes a large memory size.
Example of a Python array
array1 = [55.9, 9.8, 7.8]
array2 = [6, 7, 8, 9]
An array is a variable that contains elements belonging to the same data types. To create an array we have to declare it first by using the array module of Python. Different operations such as the addition or removal of elements from the array can be performed easily by the use of built-in functions in Python. Memory size consumed is less as compared to lists.
When to use Arrays?
Arrays are extremely useful to sort and organize the data sets of the same type in computer programs so that it is easily accessible and available when needed. Although the lists used are much more flexible, they consume a large space. However, if we use arrays, the space consumed by them will be much less than the lists.
The lists can contain values of all data types, but the array only allows values from the same data types which makes it far easier to search and access information. But if it is not necessary, the use of arrays should be avoided to reduce complexity in the program. Arrays are extremely useful when we have to perform operations on a data structure containing the same type of values.
FAQs on Python Array
Q. What is an array in Python?
A collection of homogeneous elements or items, that is, values belonging to the same data types is known as an array in Python. If the values entered in the array are of different data types then it will generate an error.
Examples for the array are:
#array containing integer values from array import * arr_int = array(i, [3, 6, 2, 9, 4, 8, 1, 5, 7, 11]) #array containing floating decimal values from array import * arr_float = array(d, [6.7, 4.4, 3.2, 5.6, 7.8])
Arrays can be modified, changed at the will of the programmer. This is because arrays are mutable. We can also bring change either to the entire array or to a specific element of the array. The addition of an element, removal of an element, slicing of the array can be done using built-in functions and python operators as explained above.
Q. How do you create an array in Python?
A Python array can be created by the use of an array module in Python. Two parameters are used in declaring an array, the first one is used to declare the data type values used in the array. The second one is the list of the elements contained by . Different data types can be entered and that too of different bytes such as int, float, double of 1, 2, 4, and 8 bytes.
Q. Is a Python list the same as an array?
Both, list and array in Python store values, but the main difference is that list allows all data types in a single data structure, whereas, in an array, only values belonging to the same data type are valid.
Q. Is an array available in Python?
Unlike a list, it cannot be readily declared. but you can create an array in Python by importing the array module first.