An array is a fundamental data structure used to store multiple elements
in an organized sequence. Arrays are important in Data Structures and
Algorithms (DSA), Data Science, Machine Learning and Artificial Intelligence.
1. What is an Array? #
Concept #
An array stores multiple elements and allows elements to be accessed using
an index. Python uses zero-based indexing, so the first element is stored
at index 0.
Example #
arr = [10, 20, 30, 40, 50]
print(arr)
Output #
[10, 20, 30, 40, 50]
Explanation #
The array contains five elements. Their indexes are 0, 1, 2, 3 and 4.
Index: 0 1 2 3 4
↓ ↓ ↓ ↓ ↓
┌────┬────┬────┬────┬────┐
Array: │ 10 │ 20 │ 30 │ 40 │ 50 │
└────┴────┴────┴────┴────┘
Complexity #
Access by index: O(1)
DSA / AI Context #
Arrays are used for storing collections of values in DSA. In Data Science
and Machine Learning, array-like structures are used to represent feature
vectors, datasets and numerical data.
2. Accessing Array Elements #
Concept #
An element can be accessed using its index.
Example #
arr = [10, 20, 30, 40, 50]
print(arr[0])
print(arr[2])
print(arr[4])
Output #
10
30
50
Explanation #
The first element is at index 0, the third element is at index 2,
and the fifth element is at index 4.
Complexity #
Time Complexity: O(1)
Space Complexity: O(1)
DSA / AI Context #
Direct indexing is frequently used when accessing a particular element,
feature, pixel or value from an array.
3. Updating an Array #
Concept #
An existing element can be replaced by assigning a new value to its index.
Example #
arr = [10, 20, 30, 40, 50]
arr[2] = 100
print(arr)
Output #
[10, 20, 100, 40, 50]
Explanation #
The value at index 2 was 30. It has been replaced with 100.
Complexity #
Time Complexity: O(1)
Space Complexity: O(1)
DSA / AI Context #
Updating is useful when modifying values during algorithmic processing
or preprocessing numerical data.
4. Traversing an Array #
Concept #
Traversal means visiting each element of an array one by one.
Example #
arr = [10, 20, 30, 40, 50]
for value in arr:
print(value)
Output #
10
20
30
40
50
Explanation #
The loop visits every element from the first element to the last element.
Complexity #
Time Complexity: O(n)
Space Complexity: O(1)
DSA / AI Context #
Traversal is used when processing every element of an array, such as
calculating statistics, searching values or processing model data.
5. Inserting Elements #
Concept #
Insertion means adding a new element to an array-like sequence.
Python lists provide methods such as append(), insert() and extend().
Example #
arr = [10, 20, 30]
arr.append(40)
print(arr)
arr.insert(1, 15)
print(arr)
arr.extend([50, 60])
print(arr)
Output #
[10, 20, 30, 40]
[10, 15, 20, 30, 40]
[10, 15, 20, 30, 40, 50, 60]
Explanation #
append() adds an element at the end,
insert() adds an element at a specified position,
and extend() adds multiple elements.
Complexity #
append(): O(1) amortized
insert(): O(n)
extend(): O(k), where k is the number of added elements
DSA / AI Context #
Insertion is common when dynamically building collections of values,
although frequent middle insertions are generally less efficient than
appending.
6. Deleting Elements #
Concept #
Deletion removes an element from an array-like sequence.
Example #
arr = [10, 20, 30, 40, 50]
arr.remove(30)
print(arr)
value = arr.pop()
print(value)
print(arr)
Output #
[10, 20, 40, 50]
50
[10, 20, 40]
Explanation #
remove() removes the first matching value.
pop() removes and returns an element.
Complexity #
remove(): O(n)
pop(): O(1) for the last element
pop(i): O(n) generally
DSA / AI Context #
Deletion is useful when removing unwanted values, filtering data or
maintaining a collection during an algorithm.
7. Searching in an Array #
Concept #
Searching means finding whether a particular value exists and,
when required, determining its position.
Example: Linear Search #
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
arr = [10, 20, 30, 40, 50]
result = linear_search(arr, 30)
print(result)
Output #
2
Explanation #
Linear search checks elements sequentially until the target is found.
Complexity #
Best Case: O(1)
Worst Case: O(n)
Space Complexity: O(1)
DSA / AI Context #
Linear search is useful for unsorted data and is one of the basic
searching techniques every DSA learner should understand.
8. Binary Search #
Concept #
Binary search repeatedly divides a sorted search space into two parts.
It requires the data to be sorted.
Example #
def binary_search(arr, target):
left = 0
right = len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
arr = [10, 20, 30, 40, 50]
print(binary_search(arr, 40))
Output #
3
Complexity #
Time Complexity: O(log n)
Space Complexity: O(1)
DSA / AI Context #
Binary search is an important algorithmic technique and appears in
many optimized array and search-space problems.
9. Two-Dimensional Array #
Concept #
A two-dimensional array organizes elements into rows and columns.
In Python, it can be represented using a list of lists.
Example #
matrix = [
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
]
print(matrix)
Output #
[[10, 20, 30], [40, 50, 60], [70, 80, 90]]
Explanation #
Column
0 1 2
Row 0 10 20 30
Row 1 40 50 60
Row 2 70 80 90
The general syntax is:
matrix[row][column]
Complexity #
Access a particular element: O(1)
Traverse entire matrix: O(rows × columns)
DSA / AI Context #
Two-dimensional arrays are used for matrices, grids, images, dynamic
programming tables and many other DSA problems.
10. Traversing a 2D Array #
Concept #
A 2D array can be traversed using nested loops.
Example #
matrix = [
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
]
for row in matrix:
for value in row:
print(value, end=" ")
print()
Output #
10 20 30
40 50 60
70 80 90
Complexity #
Time Complexity: O(rows × columns)
Space Complexity: O(1)
11. Important Python Array/List Methods #
Concept #
Python lists provide several methods that are frequently used when
solving array-based DSA problems.
Example #
arr = [10, 20, 30]
arr.append(40)
print(arr)
arr.insert(1, 15)
print(arr)
arr.extend([50, 60])
print(arr)
arr.remove(20)
print(arr)
print(arr.pop())
print(arr.index(15))
print(arr.count(10))
arr.sort()
print(arr)
arr.reverse()
print(arr)
new_arr = arr.copy()
print(new_arr)
Complexity #
append() → O(1) amortized
insert() → O(n)
extend() → O(k)
remove() → O(n)
pop() → O(1) for last element
index() → O(n)
count() → O(n)
sort() → O(n log n) typical
reverse() → O(n)
copy() → O(n)
12. Array Operation Complexity #
Complexity Table #
| Operation | Time Complexity |
|---|---|
| Access | O(1) |
| Update | O(1) |
| Traversal | O(n) |
| Linear Search | O(n) |
| Binary Search | O(log n) |
| Insertion | O(n) |
| Deletion | O(n) |
| Sorting | O(n log n) typical |
| Merging | O(n + m) |
| Splitting | O(n) |
13. Arrays in Data Science: NumPy #
Concept #
In Data Science and Machine Learning, numerical data is commonly handled
using NumPy arrays. NumPy provides the ndarray data structure.
Example #
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(arr)
print(arr.shape)
Output #
[10 20 30 40 50]
(5,)
Explanation #
The shape (5,) means the array contains five elements in one
dimension.
DSA / AI Context #
NumPy arrays are widely used for numerical calculations, feature
engineering, scientific computing and Machine Learning workflows.
14. Arrays as Feature Vectors #
Concept #
In Machine Learning, the characteristics used by a model are called
features. A collection of features for one sample can be represented
as a feature vector.
Example #
features = [25, 50000, 2]
print(features)
Explanation #
25 → Age
50000 → Salary
2 → Experience
Therefore:
[25, 50000, 2]
is a feature vector containing three features.
DSA / AI Context #
This is where the basic array concept connects directly with Machine
Learning.
15. Arrays as Dataset Matrices #
Example #
X = [
[25, 50000, 2],
[30, 70000, 5],
[22, 35000, 1]
]
print(X)
Explanation #
Rows → Samples
Columns → Features
The dataset contains three samples and three features.
NumPy Version #
import numpy as np
X = np.array([
[25, 50000, 2],
[30, 70000, 5],
[22, 35000, 1]
])
print(X.shape)
Output #
(3, 3)
16. Arrays and Tensors #
Concept #
A tensor is a multidimensional numerical structure used extensively
in Deep Learning frameworks such as TensorFlow and PyTorch.
Scalar → 0D
Vector → 1D
Matrix → 2D
Tensor → Higher-dimensional data
AI Example #
Image:
(height, width, channels)
Image Batch:
(batch_size, height, width, channels)
For example:
(32, 224, 224, 3)
This can represent 32 RGB images, each with a height and width of 224
pixels and three color channels.
17. Array Practice Problems #
- Find the largest element in an array.
- Find the smallest element.
- Find the second largest element.
- Reverse an array.
- Calculate the sum of all elements.
- Count even and odd elements.
- Search for an element.
- Remove duplicate elements.
- Move all zeros to the end.
- Rotate an array.
- Find the missing number.
- Merge two sorted arrays.
- Find the maximum subarray sum.
- Solve the Two Sum problem.
- Traverse a 2D matrix.
18. Quick Revision #
Array
│
├── Indexing
├── Traversal
├── Updating
├── Insertion
├── Deletion
├── Searching
│ ├── Linear Search
│ └── Binary Search
├── Sorting
├── 2D Arrays
├── Complexity
│
└── Data Science / AI
├── NumPy ndarray
├── Feature Vector
├── Dataset
├── Image
└── Tensor
Arrays are therefore not only a DSA topic. They form a foundation that
connects Python programming and algorithmic problem solving with Data
Science, Machine Learning and Artificial Intelligence.