Python Arrays
Python provides multiple ways to work with linear data structures, which store elements
sequentially. Although Python does not have a built-in array type like some other
languages, similar functionality can be achieved using:
Lists
Array module
NumPy arrays
An array is a container which can hold a fix number of items and these items should be
of the same type. Each item stored in an array is called an element and they can be of
any type including integers, floats, strings, etc.
These elements are stored at contiguous memory location. Each location of an element in
an array has a numerical index starting from 0. These indices are used to identify and
access the elements.
Array Representation
Arrays are represented as a collection of multiple containers where each container stores one
element. These containers are indexed from '0' to 'n-1', where n is the size of that particular
array.
Syntax: my_list = [10, "Python", 3.14]
Index starts with 0.
Python Lists
Lists are a flexible and commonly used data structure for storing elements in sequence.
Unlike arrays in other languages, list:
Can store mixed data types in one list
Elements can be added or removed easily
Provide functions like append(), remove(), sort(), etc.
a = [1, "Hello", [3.14, "world"]]
[Link](2) # Add an integer to the end
print(a)
Output
[1, 'Hello', [3.14, 'world'], 2]
NumPy Arrays
NumPy arrays are a part of the NumPy library, which is a tool for numerical computing.
Designed for high-performance operations on large datasets and support multi-
dimensional arrays and matrices, making them suitable for complex mathematical
computations.
For Data Science, Machine Learning, or heavy math, everyone uses NumPy.
import numpy as np arr = [Link]([1, 2, 3, 4])
Efficient storage and processing, Multi-dimensional support
Python Arrays
Array is a collection of elements stored at contiguous memory locations, used to hold multiple values of the same
data type. Unlike Lists, which can store mixed types, arrays are homogeneous and require a typecode during
initialization to define the data type.
import array as arr
a = [Link]('i', [1, 2, 3])
# accessing First Araay
print(a[0])
# adding element to array
[Link](5)
print(a)
Output
1
array('i', [1, 2, 3, 5])
Explanation: parameter 'i' is the typecode, it tells Python to treat the elements as signed integers ([1, 2, 3]) of a
specific byte size (usually 2 or 4 bytes).
The following table outlines standard typecodes, their mapped C types and minimum memory requirements:
Typecode C Type Python TypeMinimum Size (Bytes)
'b' signed char int 1
'i' signed int int 2
'f' float float 4
'd' double float 8
Create an Array
Array can be created by importing an array module. array(data_type, value_list) is used to create array with data type
and value list specified in its arguments.
import array as arr
a = [Link]('i', [1, 2, 3])
for i in range(0, 3):
print(a[i], end=" ")
Output
1 2 3
Adding Elements to an Array
Elements can be added to an array using insert() to place a value at a specific index, or append() to add a value at
the end.
import array as arr
a = [Link]('i', [1, 2, 3])
print(*a)
[Link](1, 4) # Insert 4 at index 1
print(*a)
//incase not used * display the result array('i', [1, 2, 3])
Output
1 2 3
1 4 2 3
Accessing Array Items
Array elements are accessed using their index with square brackets [ ]. Each item has a position starting from 0 and
the index must be an integer.
import array as arr
a = [Link]('i', [1, 2, 3, 4, 5, 6])
print(a[0])
print(a[3])
b = [Link]('d', [2.5, 3.2, 3.3])
print(b[1])
print(b[2])
Output
1
4
3.2
3.3
Removing Elements from the Array
Elements can be removed using remove(), which deletes the first occurrence of a value, or pop(), which removes and
returns an element (last by default or a specific index if provided).
import array
a = [Link]('i', [1, 2, 3, 1, 5])
# remove first occurance of 1
[Link](1)
print(a)
# remove item at index 2
[Link](2)
print(a)
Output
array('i', [2, 3, 1, 5])
array('i', [2, 3, 5])
Slicing of an Array
Slicing is used to access a specific range of elements from an array using index positions.
Elements from beginning to a range use [:Index]
Elements from end use [:-Index]
Elements from specific Index till the end use [Index:]
Elements within a range, use [Start Index:End Index]
Print complete List, use [:].
For Reverse list, use [::-1].
import array as arr
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = [Link]('i', a)
res = b[3:8]
print(res)
res = b[5:]
print(res)
res = b[:]
print(res)
Output
array('i', [4, 5, 6, 7, 8])
array('i', [6, 7, 8, 9, 10])
array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Searching Element in an Array
In order to search an element in the array we use index() method. This function returns the index of the first occurrence of value
mentioned in arguments.
import array
a = [Link]('i', [1, 2, 3, 1, 2, 5])
# index of 1st occurrence of 2
print([Link](2)) // 2 is array element value
# index of 1st occurrence of 1
print([Link](1)) //find index of 1
Output
1
0
Updating Elements in an Array
In order to update an element in the array we simply reassign a new value to the desired index we want to update.
import array
a = [Link]('i', [1, 2, 3, 1, 2, 5])
# update item at index 2
a[2] = 6
print(a)
# update item at index 4
a[4] = 8
print(a)
Output
array('i', [1, 2, 6, 1, 2, 5])
array('i', [1, 2, 6, 1, 8, 5])
Arrays Operations
1. Counting Elements in an Array: We can use count() method to count given item in array.
import array
a = [Link]('i', [1, 2, 2,3, 4, 2, 5, 2])
count = [Link](2)
print(count)
Output
4
2. Reversing Elements in an Array: In order to reverse elements of an array use reverse method.
import array
a = [Link]('i', [1, 2, 3, 4, 5])
[Link]()
print(*a)
Output
5 4 3 2 1
3. Extend Element from Array: extend() function is used to attach an item from iterable to the end of the array. This
method is used to add an array of values to the end of a given or existing array.
import array as arr
a = [Link]('i', [1, 2, 3,4,5])
[Link]([6,7,8,9,10])
print(a)
Output
array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])