Arrays in Python

An array is a collection of data of the same type stored in contiguous memory locations. The fact that the data items are stored in simultaneous memory locations enables us to calculate the locations of data items by adding an offset to the base address of the array.

Arrays are ordered, indexed, and mutable data structures. Thus data items can be accessed via indexes. Arrays are one of the most important data structures that exist, and most other data structures have been built on the basic framework that arrays provide.
d9

Arrays in Python

Arrays in Python can be implemented using the built-in array package. The array package can be imported from the Python libraries and then used.

from array import *
Creation

The array.array() function can be used to construct Python arrays. The syntax to create an array is as follows,

<array variable> = array.array( <typecode> , <initializing elements> )

The typecode refers to the data type of the items, the particular array will hold.

The typecodes available for usage are,

Typecode Description
b Signed integer of 1 byte
B Unsigned integer of 1 byte
c Character of 1 byte
i Signed integer of 2 bytes
I Unsigned integer of 2 bytes
f Floating-point of 4 bytes
d Floating-point of 8 bytes

Usage of the array() function is as follows,

import array
a = array.array('i', [1, 2, 3])
print(a)

output

array('i', [1, 2, 3])

Features of an array

  1. Ordered - items in an array are stored in a certain order.
  2. Indexed - each element in an array has a particular index assigned to it. Adding the index offset to the base location will give us the location of the item with the given index. Indexes
  3. Mutable - Array items are mutable. That is changeable.
  4. Iterable - array items can be accessed through iteration. That is arrays are iterable data structures.
  5. The len() function can be used to calculate the length of the array.
  6. Items stored in an array should be of the same data type for a particular array.
  7. Duplicacy allowed - Duplicate items are allowed in an array.

Basic operations supported by arrays

  • Insertion - items of the same data type can be inserted into arrays
  • Deletion - Items from an array can be deleted
  • Searching - items in an array can be searched via index or via value
  • Updation - items in particular indexes can be updated
  • Traversal - items of an array can be traversed

Accessing elements of an array

Arrays are indexed data structures. Thus the elements of a Python array can be accessed via index.

Python arrays are indexed from 0 till the length of the array - 1.

arr = array.array('i', [1, 2, 3])
print(arr[0])
print(arr[2])

output

1
3
Slicing an array

An array can be sliced into smaller sections like Python lists, using indexes.

arr = array.array('i', [1, 50, 2, 3, 60])
print(arr[1:3])

output

array('i', [50, 2])

If one of the ends of the two indexes given during slicing is left empty, then it takes up that entire end of the array.

arr = array.array('i', [1, 50, 2, 3, 60])
print(arr[2:])
print(arr[:3])

output

array('i', [2, 3, 60])
array('i', [1, 50, 2])

Insertion

Elements can be added to the beginning, end, or any position inside an array. Elements can also be updated in an array.

insert()

The insert() function can be used to insert one or more elements into an array.

Syntax:

<array variable>.insert( <index to be inserted into> , <element to be inserted> )

for example

arr.insert(1, 50)
print(arr)

output

array('i', [1, 50, 2, 3])
append()

The append() function can be used to add elements to the end of the array.

arr.append(60)
print(arr)

output

array('i', [1, 50, 2, 3, 60])
Updation

Elements in an array can be updated by assigning new values to exist positions by specifying their indexes.

arr[2] = 20

output

array('i', [1, 50, 20])

Deletion

Arrays in Python allow deletion of its elements. After deletion, the elements of the array are rearranged with their indexes.

The remove() function is used to delete elements in an array by specifying the value.

arr.remove(2)

output

array('i', [1, 50, 3, 60])

The pop() function can be used to delete the last inserted element into the array.

arr.pop()

output

array('i', [1, 50, 3])

Searching for an element

Searching for an element in an array can be achieved via indexes or values.

The index() function is used to find the index in the array of the value specified.

a = arr.index(50)
print(a)

output

1
Searching for an element not present in the array will lead to an error.
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions