Python Arrays Class
Python Arrays Class
Zappcode Academy
PYTHON ARRAY
An array is a collection of items stored at contiguous memory locations. The idea is
to store multiple items of the same type together. This makes it easier to calculate the
position of each element by simply adding an offset to a base value, i.e., the memory
location of the first element of the array (generally denoted by the name of the
array).
A combination of the arrays could save a lot of time by reducing the overall size of
the code.
In Short, Arrays are used to store multiple values in one single variable.
Memory Efficiency: Arrays in Python (from the array module or NumPy arrays)
can be more memory-efficient compared to lists, especially when dealing with large
datasets of a single data type.
Performance: Arrays can provide better performance for certain operations
compared to lists, especially when dealing with numerical computations.
CREATE AN ARRAY
import array
import numpy as np
# Python List
py_list = [1, 2, 3, 4, 5]
# Array Module
arr = array.array('i', [1, 2, 3, 4, 5])
# NumPy Array
np_arr = np.array([1, 2, 3, 4, 5])
print(py_list)
print(arr)
print(np_arr)
CREATING ARRAY OF
UNICODE CHARACTERS
import array
# Create an array of Unicode characters
char = array.array('u', ['a', 'b', 'c', 'd', 'e'])
print(char)
REPLACING AND ADDING
NEW ITEM
import array
unicode_arr = array.array('u', ['a', 'b', 'c', 'd', 'e'])
# Access elements
print(unicode_arr[0])
# Modify elements
unicode_arr[1] = 'z'
print(unicode_arr)
# Append new Unicode character
unicode_arr.append('f')
print(unicode_arr)
DIFFERENCE BETWEEN
ARRAYS AND LIST
A List can contain items of different data types whereas an array cannot contain
items of different data types.
An array can have only single data type i.e. either strings, numbers, Boolean, etc
Both list and array gives the same output, but when we have large data, we tend to
use an array.
List takes more space when compared to an Array.
TRYING ANOTHER PROGRAM
#accessing of element from list
import array as arr
# array with int type
a = arr.array('i', [1, 2, 3, 4, 5, 6])
print("Access element is:", a[0])
print("Access element is:", a[3])
Output:
Access element is: 1
Access element is: 4
ACCESSING ARRAY
ELEMENTS
import array
a = array.array('i', [1, 2, 3, 4, 5])
# Accessing elements
print("Array elements:")
for num in a:
print(num)
# Accessing elements by index
print("\nAccessing element at index 2:", a[2])
ADDING IN AN EMPTY ARRAY
import array
a = array.array('u')
# Add characters to the array
a.append('a')
a.append('b')
a.append('c')
# Display the array
print("Array after adding elements:", a)
REMOVING ELEMENTS IN AN
ARRAY
import array
a = array.array('f', [1, 2, 3, 4, 5])
print("Original Array:", a)
# Remove an element (3.3 in this case)
a.remove(3)
# Display the array after removal
print("Array after removing element:", a)
ADDING ELEMENTS TO AN
ARRAY
Elements can be added to the Array by using built-in insert() function.
Insert is used to insert one or more data elements into an array. Based on the
requirement, a new element can be added at the beginning, end, or any given index
of array.
append() is also used to add the value mentioned in its arguments at the end of the
array.
Let’s give it a try.
USING INSERT TO ADD A NEW
ITEM
import array
a = array.array('i', [1, 2, 3, 4, 5])
# Display the original array
print("Original Array:", a)
a.insert(2, 100)
# Display the modified array
print("Array after insertion:", a)
REPLACING AN ELEMENT
import array
a = array.array('i', [10, 20, 30, 40, 50])
print("Original Array:", a)
# Update an element at index 2
a[2] = 35
# Display the array after update
print("Array after updating element:", a)
ADDING USING EXTEND
METHOD
import array
a = array.array('i', [1, 2, 3])
b = array.array('i', [4, 5, 6])
# Extend a with elements from b
a.extend(b)
print("Extended Array:", a)
SORTING ARRAY
import array
a = array.array('i', [5, 3, 7, 2, 8, 1, 4, 6])
# Sort the array
b = sorted(a)
print("Sorted Array:", b)
USING POP() METHOD
#removal of array elements using pop method
fruits = ['apple', 'banana', 'cherry']
fruits.pop(1)
print(fruits)
Output:
['apple', 'cherry']
IN PYTHON ARRAY, THERE
ARE MULTIPLE WAYS TO
PRINT THE WHOLE ARRAY
WITH ALL THE ELEMENTS,
BUT TO PRINT A SPECIFIC
RANGE OF ELEMENTS FROM
THE ARRAY, WE USE SLICE
OPERATION.
USING SLICING
#using slicing method
fruits = ['apple', 'banana', 'cherry', 'kiwi', 'peach']
print(fruits[0:1])
print(fruits[0:])
print(fruits[:1])
print(fruits[:])
SEARCHING ELEMENT IN A
ARRAY
In order to search an element in the array we use a python in-built index() method.
This function returns the index of the first occurrence of value mentioned in
arguments.
Output:
The index of 1st occurrence of peach is : 2