Arrays in Python

As we know, an array contains data belonging to the same type. In Python, an array is a group of numeric values. Although, there is no built-in datatype like arrays in Python but a module named array is available which is used for array creation.

Difference between Python Array and Python Lists

Oftentimes, python lists are mistakenly considered as arrays. But they both are different. Lists are capable of holding multiple datatypes whereas arrays are not. Arrays can only store numeric values like integer, float etc.

Apart from this, lists and arrays are created using different approaches. A module called as array is used for creating arrays in Python.

However, python arrays are not in much use because they are less flexible than lists and a little slower.

This article will tell you about:

How to create an array in Python?

Python provides a module array for array creation. This module can be imported from the python library to create an array.

Syntax:
import array as arr
array_name = array( typecode, [elements.. ] )

  • import: This keyword is used to import any module.
  • array: This is the module name.
  • arr: The module is imported as arr. You can use any name in place of arr.
  • Typecode: This specifies the datatype of values that the array will hold.
  • Elements: The array elements are enclosed within a pair of square brackets. All the elements must belong to the same type specified as typecode.

Example:

import array as num
number = num.array('i',[11,22,33,44,55]) 
print(number)

Output:

array('i', [11, 22, 33, 44, 55])

In the above example, the typecode 'i' is used for integer values. Therefore, all the elements present inside the square brackets are integers.

Even if a single element does not belong to the given datatype, an error will be generated.

Example:

import array as num
number = num.array('i',[11,2.5,33,44,55])
print(number)

Output: error.

Traceback (most recent call last):
File "C:/Users/TS7/AppData/Local/Programs/Python/Python37/array.py", line 2, in <module>
number = num.array('i',[11,2.5,33,44,55])
TypeError: integer argument expected, got float


Typecodes for different datatypes in python:

How to access array elements in Python?

An array is an indexed datatype, thus you can access its elements with the help of the index. Like lists in Python, the first element of the array is indexed 0.

Syntax:
array_name[index]

Example:

import array as num
number = num.array('i',[11,22,33,44,55]) 
print(number)
print('Accessing all the array elements one by one.')
print('number[0] =', number[0])
print('number[1] =', number[1])
print('number[2] =', number[2])
print('number[3] =', number[3])
print('number[4] =', number[4])

Output:

array('i', [11, 22, 33, 44, 55])
Accessing all the array elements one by one.
number[0] = 11
number[1] = 22
number[2] = 33
number[3] = 44
number[4] = 55

Slicing in Python Array

Arrays in Python can be sliced so that you can access a part of the array instead of one element at a time.

Example:

import array as num
number = num.array('i',[11,22,33,44,55]) 
print(number)
print('Accessing a part of the array')
print('number[1:4] =', number[1:4])

Output:

array('i', [11, 22, 33, 44, 55])
Accessing a part of the array
number[1:4] = array('i', [22, 33, 44])

How to change the existing elements in Python Arrays?

Since Arrays are mutable (changeable), you can change the existing elements with new ones. This can be carried out using the index of the elements.

Syntax:
array_name[index] = new value

Example:

import array as num
number = num.array('i',[11,22,33,44,55]) 
print(number)

#changing the fourth element
number[3] = 100
print('After changing: ', number)

Output:

array('i', [11, 22, 33, 44, 55])
After changing: array('i', [11, 22, 33, 100, 55])

How to add elements in Python Array?

The length of the array can be increased by adding new elements to it. Python provides some built-in functions for this task. They are:

  • append(): With this function, you can add a single element at the end of the array.
    Argument to the function: The new element is passed as the argument.

    Syntax:
    array_name.append(new element)

  • extend(): This function lets you add more than one element in one go. In this case also, all the elements are added after the last element.
    Argument to the function: An iterable (lists, tuple etc) containing the new elements is passed as the argument.

    Syntax:
    array_name.extend(new elements)

Example:

import array as num
number = num.array('i',[11,22,33,44,55]) 
print(number)

#using append()
number.append(66)
print(number)

#using extend()
number.extend([77,88,99])
print(number)

Output:

array('i', [11, 22, 33, 44, 55])
array('i', [11, 22, 33, 44, 55, 66])
array('i', [11, 22, 33, 44, 55, 66, 77, 88, 99])

How to delete elements from Python Array?

Deletion of array elements can be done in the following three ways.

  • pop() function: This is a built-in function which removes an element whose index is given as the argument to the function.

    Syntax:
    number.pop(index)

  • remove() function: This function takes the elements itself as an argument and removes it from the array.

    Syntax:
    number.remove(element)

  • del Keyword: You can also use the del keyword to delete either the array elements or the whole array.

    Syntax:
    del array_name[index] 
    or
    del array_name 

Example:

import array as num
number = num.array('i',[11,22,33,44,55]) 
print(number)

#using pop() function.
number.pop(0)
print('Element at index 0 deleted.\n', number)

#using remove() function.
number.remove(55)
print('Element 55 deleted.\n',number)

#using del keyword to delete one element.
del number[2]
print('Element at index 2 deleted.\n',number)

#using del keyword to delete one element.
del number
print(number)

Output:

array('i', [11, 22, 33, 44, 55])

Element at index 0 deleted.
array('i', [22, 33, 44, 55])

Element 55 deleted.
array('i', [22, 33, 44])

Element at index 2 deleted.
array('i', [22, 33])

Traceback (most recent call last):
File "C:/Users/TS7/AppData/Local/Programs/Python/Python37/array1.py", line 15, in <module>
print(number)
NameError: name 'number' is not defined

In the output, the error arises due to the last print statement because the statement 'del number' has already deleted the entire array from the memory. As a result, NameError occurs, stating that the name 'number' is not defined.

Concatenation of Python Arrays

Python allows you to connect two or more arrays back to back with the + operator. As a result, a new array is produced which holds the concatenated array.

Example:

import array as a

odd = a.array('i',[1,3,5,7,9]) 
print('odd =', odd)

even = a.array('i',[2,4,6,8]) 
print('even =', even)

new_array = odd + even
print('new_array =', new_array)

Output:

odd = array('i', [1, 3, 5, 7, 9])

even = array('i', [2, 4, 6, 8])

new_array = array('i', [1, 3, 5, 7, 9, 2, 4, 6, 8])