Arrays

Python's built-in data structures do not include the array.
The "array" module must therefore be imported before we can create or use an array.

import array as arr 

ordered, mutable collections, type restricted


link - docs.python.org/3/library/array.html 

Multi-dimensional Arrays

If you want to create multi-dimensional arrays you need to use the NumPy Package



my_array = [] 
if my_array:
print("Array has items")
else:
print("Array is empty.")

my_array = [10, 10, 20, 1, 1]
if my_array:
print("Array has items")
else:
print("Array is empty.")


The square brackets are also printed

names = ["Alice", "Bob", "Carol"] 
print(names) #= ['Alice', 'Bob', 'Carol']




accessing First Araay

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


adding element to array

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



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

for i in range(0, 3):
    print(a[i], end=" ") #= 1 2 3


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


Insert 4 at index 1

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


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


b = arr.array('d', [2.5, 3.2, 3.3])  
print(b[1]) #= 3.2
print(b[2]) #= 3.3


remove first occurance of 1

a = array.array('i', [1, 2, 3, 1, 5])  
a.remove(1)
print(a) #= array('i', [2, 3, 1, 5])

remove item at index 2

a = array.array('i', [1, 2, 3, 1, 5])  
a.pop(2)
print(a) #= array('i', [2, 3, 5])


slicing an array

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


a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
res = a[5:]
print(res) #= [6, 7, 8, 9, 10]



a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
res = a[:]
print(res) #= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]




© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext