Python Array Examples

What is an array in Python?

Arrays are the best option whenever it is necessary to use several elements of the same data type simultaneously. An array is a group of identically typed elements. An “array” is the most common data structure that can store items of the same type. 

Mostly it is helpful in loops, branches etc. when there is a need to access a particular element from a group of the same datatype elements or to make use of the same datatype elements at a time. Arrays are specialized versions of collections of data elements that are ordered and ordered in a specific way.

In Python, arrays consume less memory than lists and are faster. Python takes advantage of both the speed and memory efficiency of an array.

Arrays are one of the most popular data structures. It is used to organize data in large collections. The elements in the array can be of any type: integers, floating points, strings, etc. but of the same data type.

Declaration of arrays:

Import array modules:

An array can be declared by importing an array module, as arrays are not the fundamental, built-in data types in Python.

Arrays can be imported into Python by three means:

  1. Importing the array module: importing the “import array” followed by array.array() for array creation.
import array;
array.array // creating array

Syntax

arrayName = array.array(dataType, [array items])
  1. Importing array as arr; “import array as arr” which is followed by arr.array() for array creation.
import array as arr;
arr.array() // creating array
  1. Importing the array with “from array *” which also imports all the array functionalities in python.
from array import *
array(); creating array

Syntax:

variable_name = array(typecode,[elements])

  • variable_name is the name of the array which is named just similar to the naming of any variables in python.
  • The typecode indicates the type of the elements that will be stored in the array like; integers, floating points etc.
  • Inside the square brackets array of elements are placed.

Also Read Top Python Tools

Finding the length of an array:

For finding the length of the array, one can use the built-in array method; len().

The len() returns the numerical value of the length of the array.

import array as arr
numbers=arr.array('i',[10,20,30])
print(len(numbers))

Output: 3

Indexing and accessing the individual elements in an array:

Through array indexing, we can access the elements in an array. Accessing the elements can be of single element access or multiple elements access. 

import array as arr
numbers=arr.array('i',[10,20,30])
print(len(numbers))

print(numbers[0])
print(numbers[1])
print(numbers[2])
import array as arr
numbers=arr.array('i',[10,20,30])
print(len(numbers))

print(numbers[0])
print(numbers[1])
print(numbers[2])

3
10
20
30

Note: The important point to be noted here is, that the last element in the array is always one minus the length of the array, that is the indexing starting from integer value 0 to length-1.

Accessing elements can also be done through negative indexing where the last element carries the index value as -1 and the -2 for the last but one and so on. 

import array as arr

numbers=arr.array('i', [10,20,30])
print(numbers[-1])
print(numbers[-2])
print(numbers[-3])

30
20
10

Finding the index value of an element in the array:

We can find the index value of the element in the array through index() method. The element whose index is needed is passed as an argument in the method which in return gets the index value of the provided element. 

import array as arr
numbers=arr.array('i', [10,20,30])
print(numbers.index(10)) // 0

Adding a new element to the array:

We can add a new element to the array through the append() method. Only a single element can be added at the last indexed position of the array of the same data type. 

import array as arr

numbers=arr.array('i', [10,20,30])
numbers.append(40)
print(numbers)

// array('i', [10, 20, 30, 40])

Adding multiple elements to an array:

More than one element of the same data type can be added at the end of the array through extend() method. 

import array as arr

numbers=arr.array('i', [10,20,30])
numbers.extend([40,50,60])
print(numbers)

// array('i', [10, 20, 30, 40, 50, 60])

Adding an element at a particular position:

Sometimes we don’t want to add elements at the end of the array, instead, we refer to adding the elements at some fixed position. The insert() method is used to add the elements at the desired position by taking up the arguments for the index position and the new element value. 

import array as arr

numbers=arr.array('i', [10,20,30])
numbers.insert(0,40)
print(numbers)

//array('i', [40, 10, 20, 30])

Removing an element from the array:

We can remove an element from the array by simply using the remove() method where the element that needs to be removed is passed as an argument in the method. 

import array as arr

numbers=arr.array('i', [10,20,30])
numbers.remove(10)
print(numbers)

// array('i', [20, 30])

remove () method effectively removes the desired elements, but what if the array consists of identical elements or identical element values? In this case, the remove() removes the first occurrence of the element in the array. To understand this, let us look at an example.

In the below example, the element value is 10 with two times occurrence in the array. Hence when the remove() method is used, the element value of 10 with the least index position is removed from the array. That is the index position 

import array as arr

numbers=arr.array('i', [10,20,30,10,20])
numbers.remove(10)
print(numbers)

// array('i', [20, 30, 10, 20])

Removing of the element from the array:

Even though the remove() method effectively removes the element from the array, when it requires removing identical elements, then the best option is to opt for the index value to remove the element. The pop() method is used to remove an element from the array using the index position of the element from the array.

import array as arr
numbers=arr.array('i', [10,20,30,10,20])
numbers.pop(0)
print(numbers)

// array('i', [20, 30, 10, 20])

Addition of elements from a list to an array:

We can add elements of a list to the array by using the method fromlist(). Here in the example, the elements of the list are added to the array at the end of the array or the last index position of the array.

import array as arr

numbers=arr.array('i', [10,20,30,10,20])
c=[11,12,13]
numbers.fromlist(c)
print(numbers)

// array('i', [10, 20, 30, 10, 20, 11, 12, 13])

Reversing an array:

An array can be reversed with the help of the reverse() method in python. While reversing the array [1, 2, 3] the array returned will be [3, 2, 1].

import array as arr

numbers=arr.array('i', [10,20,30,10,20])
c=[11,12,13]
numbers.fromlist(c)
numbers.reverse()
print(numbers)

// array('i', [13, 12, 11, 20, 10, 30, 20, 10])

Know the occurrence of the element in the array:

When it comes to knowing the count of the element occurrences in the array, the count () method is used. Count () gives the total number of times that a given element occurs in the array; that is, the number of times an element appears at index position ‘i’ in the array. 

import array as arr

a=arr.array('i',[1,1,1,2,3,4])
print(a.count(1))

// 3

Array to string conversion:

An array can be converted into a string with the help of the tostring() method in Python.

Array to list conversion:

An array can be converted into a list in python through tolist() method.

import array as arr

a=arr.array('i',[1,1,1,2,3,4])
print(a.tolist())

// [1, 1, 1, 2, 3, 4]

What we have learnt so far?

Arrays are specialized versions of collections of data elements that are the same datatype and ordered in a specific way. Python takes advantage of both the speed and memory efficiency of an array. The elements in an array can be of any type: integers, floating points, strings, etc.

Various array methods are used to operate on array elements. Methods like len(), append(), count(), reverse(), tolist(), tostring(), etc are the few of the operations on arrays that are available in python.

Hope you found this Python Array Examples article helpful.

0 Shares:
You May Also Like

Top Python Tools

Python is the latest booming programming language for beginners as well as for heads-over-heels experts. What actually made…