Arrays In Python & Operations on Arrays | FACE Prep

Arrays in Python are different from lists. Lists are the collection of elements of different data types. Whereas, arrays are the collection of elements of same data type. To begin with, let us see how we can easily access data using arrays in Python.

Why do we need Arrays?

Arrays are used to store multiple values of same data type in one single variable. For example, consider you need to add 100 integer values. To do this, you will store all the 100 values in 100 variables as shown below.

num1 = 23
num2 = 36
num3 = 41 and so on until num100 = 92

Declaring and defining 100 variables is a tedious process. So, instead of using 100 variables, we can use a single array to store 100 values. Let us move ahead and see how to create an array.

Creating an Array

In Python, an array is created by using the method ‘array()’. This method is pre-defined in the built-in module named ‘array’ and it takes two arguments. One is the data type of the array elements and the other is the array elements. Below is the syntax for creating the arrays in Python 3.

Syntax:
from array import *
array_name = array('datatype', [array_elements])

Here, data type must be mentioned within quotes (single or double) and array elements must be enclosed within square brackets as comma-separated values.

Example:
from array import *
a = array(‘i’, [2, 3, 5, 6])

Here, we have created an array of integer values. So, we have mentioned the data type as ‘i’. We can also create the array with float, double values. Below is a table consisting of commonly used data types.

NameData type
Signed int‘i’
Unsigned int‘I’
Signed long‘l’
Unsigned long‘L’
Float‘f’
Double‘d’
Unicode‘c’

Next, let us try to print values stored in the array.

Accessing Array Elements

An array element is accessed using a square bracket and an index value of the element. In Python, the array index starts from the value 0, i.e., we can access the first element using the index value 0. In the below code, the array ‘a’ consists of Unicode characters and the second element is printed using the index value 1.

from array import *
a = array('u', ['F', 'A', 'C', 'E'])
print(a[1])
Output:
A

arrays in python

We can also modify/change the array elements by simply assigning the new values to the particular index.

Modifying Array Elements

In the below code, the array ‘a’ is initialized with the float values. The original value at index 2 is 3.3. But, it has been modified to 4.4 in line #3. Only the element at the particular index gets modified. The remaining array elements do not get modified as shown below.

from array import *
a = array('f', [1.1, 2.2, 3.3])
a[2] = 4.4  #modifying array element at the index 2 as 4.4
print(a[2])  #printing the modified value
print(a[0])
print(a[1])
Output:
4.400000095367432
1.100000023841858
2.200000047683716

Instead of using separate print statements for printing each and every array element, we can use a ‘for’ loop. 

‘for’ Loop in Array

We can use the ‘for’ loop to print all the array elements using a single print statement as shown in the below example. 

from array import *
a = array('i', [10, 20, 30])
for x in a:
    print(x, end = ' ')
Output:
10 20 30

If we need to add one or more elements to the array, we can use the method ‘append()’. 

Appending new elements into an Array

An array can be appended by providing new elements as the arguments to the ‘append()’ method. In the below example, the array ‘a’ is appended with the value 40 at the last index.

Note: We can pass only one argument to the ‘append()’ method.

from array import *
a = array('i', [10, 20, 30])
a.append(40)
for x in a:
    print(x, end = ' ')
Output:
10 20 30 40

If we need to delete an element from the array, we can use the method ‘pop()’. 

Deleting an Element from an Array

An element can be deleted from an array using two different methods.

a) pop()

Using this method, an element can be deleted by specifying the index value of the element. In the below example, the element at the zeroth index is deleted from the array. 

from array import *
a = array('i', [10, 20, 30])
a.pop(0)
for x in a:
    print(x, end = ' ')
Output:
20 30

b) remove()

Using this method, an element can be deleted by specifying the element itself. In the below example, the element ’20’ is deleted from the array. 

from array import *
a = array('i', [10, 20, 30])
a.remove(20)
for x in a:
    print(x, end = ' ')
Output:
10 30

The number of elements of an array can be obtained by using the method ‘len()’ as shown in the below example.

from array import *
a = array('i', [10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
res = len(a)
print(res)
Output:
10

Arrays In Python FAQs

Arrays in Python are the collection of elements of same data type. Arrays are used to store multiple values of the same data type in one single variable. 

In Python, an array is created by using the method ‘array()’. This method is pre-defined in the built-in module named ‘array’ and it takes two arguments. One is the data type of the array elements and the other is the array elements. Below is the syntax for creating the array in Python 3.

Syntax:
from array import *
array_name = array('datatype', [array_elements])

Double values in the array can be defined using the data type ‘d’ as shown in the below example.

from array import *
a = array('d', [10.34, 20.56, 30.78])
for x in a:
    print(x, end = ' ')

Output:
10.34 20.56 30.78