0% found this document useful (0 votes)
29 views10 pages

Array in Python

Uploaded by

Yashwant Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views10 pages

Array in Python

Uploaded by

Yashwant Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Lists in Python are the most flexible and commonly used data structure for sequential storage.

They are similar to


arrays in other languages but with several key differences:
•Dynamic Typing: Python lists can hold elements of different types in the same list. We can have an integer, a
string and even other lists all stored within a single list.
•Dynamic Resizing: Lists are dynamically resized, meaning you can add or remove elements without declaring the
size of the list upfront.
•Built-in Methods: Python lists come with numerous built-in methods that allow for easy manipulation of the
elements within them, including methods for appending, removing, sorting and reversing elements.

a = [1, “Hello”, [3.14, “World”]]


a.append(2) #add an integer to the end
Print(a)
NumPy Arrays
NumPy arrays are a part of the NumPy library, which is a powerful tool for numerical computing in Python.
These arrays are designed for high-performance operations on large volumes of data and support multi-
dimensional arrays and matrices. This makes them ideal for complex mathematical computations and large-
scale data processing.
Key Features:
•Multi-dimensional support: NumPy arrays can handle more than one dimension, making them suitable for
matrix operations and more complex mathematical constructs.
•Broad broadcasting capabilities: They can perform operations between arrays of different sizes and shapes, a
feature known as broadcasting.
•Efficient storage and processing: NumPy arrays are stored more efficiently than Python lists and provide
optimized performance for numerical operations.
Import numpy as np
a = np.array([1,2,3,4])

#element-wise operations
Print(a*2)

#multi-dimensional array
res = np.array([[1,2], [3,4]])
Print(res*2)
Python Arrays
In Python, array is a collection of items stored at contiguous memory locations. The idea is to store multiple
items of the same type together. Unlike Python lists (can store elements of mixed types), arrays must have
all elements of same type. Having only homogeneous elements makes it memory-efficient.

Import array as arr


a = arr.array(‘I’, [1,2,3])

#accessing First array


Print(a[0])

#adding element to array


a.append(5)
Print(a)
Create an Array in Python
Array in Python can be created by importing an array module. array( data_type , value_list ) is used to create
array in Python with data type and value list specified in its arguments.

import array as arr


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

for i in range(0, 3):


print(a[i], end=" ")
Adding Elements to an Array
Elements can be added to the Python 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
Python array.

import array as arr


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

a.insert(1, 4) # Insert 4 at index 1


print(*a)
Accessing Array Items
In order to access the array items refer to the index number. Use the index operator [ ] to access an
item in a array in Python. The index must be an integer.

import array as arr


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

print(a[0])
print(a[3])

b = arr.array('d', [2.5, 3.2, 3.3])


print(b[1])
print(b[2])
Removing Elements from the Array
Elements can be removed from the Python array by using built-in remove() function. It will raise an Error if
element doesn’t exist. Remove() method only removes the first occurrence of the searched element. To
remove range of elements, we can use an iterator.
pop() function can also be used to remove and return an element from the array. By default it removes
only the last element of the array. To remove element from a specific position, index of that item is passed
as an argument to pop() method.

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

# remove first occurance of 1


a.remove(1)
print(a)

# remove item at index 2


a.pop(2)
print(a)
Slicing of an Array
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 .

import array as arr


a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = arr.array('i', a)

res = a[3:8]
print(res)

res = a[5:]
print(res)

res = a[:]
print(res)
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 = array.array('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)

You might also like