Python Programming Arrays
1D & 2D Arrays NumPy

Python Arrays Complete Guide

Learn Python arrays - 1D arrays, 2D arrays, NumPy arrays with practical examples, array operations, slicing techniques, and real-world data science applications.

1D Arrays

Single dimension

2D Arrays

Matrix/Grid

NumPy Arrays

Scientific computing

Array Slicing

Access subarrays

What are Arrays in Python?

Arrays are data structures that store multiple items of the same type in contiguous memory locations. In Python, lists are commonly used as arrays, but Python also has a dedicated array module and powerful NumPy arrays for numerical computing.

Key Concept

Python doesn't have built-in array support like other languages, but lists serve as dynamic arrays. For numerical computations, NumPy arrays are the industry standard due to their performance and functionality.

Different Types of Arrays in Python
# Python List as Array (most common)
list_array = [10, 20, 30, 40, 50]

# Using array module (for homogeneous data)
import array
arr = array.array('i', [1, 2, 3, 4, 5])  # 'i' for integers

# NumPy arrays (for scientific computing)
import numpy as np
numpy_arr = np.array([1, 2, 3, 4, 5])

# 2D Arrays (using lists of lists)
matrix_2d = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

print("List as array:", list_array)
print("Array module:", arr)
print("NumPy array:", numpy_arr)
print("2D Array:", matrix_2d)
Memory Representation of 1D Array

Arrays store elements in contiguous memory locations:

10
0x1000
20
0x1004
30
0x1008
40
0x1012
50
0x1016

Index: [0] [1] [2] [3] [4]

1D Arrays

1D Arrays (Single Dimension)

1D arrays are linear collections of elements accessed by a single index. In Python, we typically use lists as 1D arrays.

# Creating 1D arrays
array_name = [value1, value2, value3, ...]

# Accessing elements
element = array_name[index]

# Length of array
length = len(array_name)
Visualizing 1D Array
[0] 10
[1] 20
[2] 30
[3] 40
[4] 50

Index starts at 0, ends at length-1

1D Array Operations
# Creating 1D arrays
numbers = [10, 20, 30, 40, 50]
fruits = ["apple", "banana", "cherry"]
mixed = [1, "hello", 3.14, True]  # Python lists can hold different types

# Accessing elements
print("First element:", numbers[0])      # 10
print("Last element:", numbers[-1])      # 50 (negative indexing)
print("Second element:", fruits[1])      # banana

# Modifying elements
numbers[2] = 35       # Change 30 to 35
fruits[0] = "orange"  # Change "apple" to "orange"
print("Modified numbers:", numbers)
print("Modified fruits:", fruits)

# Array length
print("Length of numbers:", len(numbers))      # 5
print("Length of fruits:", len(fruits))        # 3

# Adding elements
numbers.append(60)            # Add to end
numbers.insert(2, 25)         # Insert at index 2
print("After adding:", numbers)

# Removing elements
removed = numbers.pop()       # Remove last element
print("Removed:", removed)
numbers.remove(20)            # Remove first occurrence of 20
print("After removal:", numbers)

# Slicing arrays
print("First 3 elements:", numbers[:3])      # [10, 25, 35]
print("Last 3 elements:", numbers[-3:])      # [35, 40, 50]
print("Elements 1-3:", numbers[1:4])         # [25, 35, 40]
print("Every other element:", numbers[::2])  # [10, 35, 50]

# Array concatenation
array1 = [1, 2, 3]
array2 = [4, 5, 6]
combined = array1 + array2
print("Combined array:", combined)  # [1, 2, 3, 4, 5, 6]

# Array repetition
repeated = [1, 2] * 3
print("Repeated array:", repeated)  # [1, 2, 1, 2, 1, 2]

# Finding elements
if 35 in numbers:
    print("35 found in array at index:", numbers.index(35))

# Counting occurrences
count_array = [1, 2, 3, 2, 1, 2, 3, 2, 1]
print("Count of 2:", count_array.count(2))  # 4
Key Points:
  • Python lists are dynamic arrays - they can grow/shrink as needed
  • Array indices start at 0 (zero-based indexing)
  • Negative indices count from the end (-1 = last element)
  • Slicing syntax: array[start:stop:step]
  • Lists can hold elements of different data types
2D Arrays

2D Arrays (Multi-dimensional)

2D arrays are arrays of arrays, often used to represent matrices, tables, or grids. In Python, we create 2D arrays using lists of lists.

# Creating 2D arrays
matrix = [[row1_col1, row1_col2], [row2_col1, row2_col2]]

# Accessing elements
element = matrix[row_index][column_index]

# Dimensions
rows = len(matrix)
cols = len(matrix[0])
Visualizing 2D Array (3x3 Matrix)
[0,0]
1
[0,1]
2
[0,2]
3
[1,0]
4
[1,1]
5
[1,2]
6
[2,0]
7
[2,1]
8
[2,2]
9

Access using matrix[row][column] where row and column indices start at 0

2D Array Operations
# Creating 2D arrays (lists of lists)
matrix = [
    [1, 2, 3],      # Row 0
    [4, 5, 6],      # Row 1
    [7, 8, 9]       # Row 2
]

# Alternative way to create 2D array
rows, cols = 3, 4
matrix2 = [[0 for _ in range(cols)] for _ in range(rows)]
print("3x4 Zero matrix:", matrix2)

# Accessing elements
print("Element at [0][0]:", matrix[0][0])   # 1
print("Element at [1][2]:", matrix[1][2])   # 6
print("Element at [2][1]:", matrix[2][1])   # 8

# Modifying elements
matrix[0][1] = 20      # Change 2 to 20
matrix[2][2] = 90      # Change 9 to 90
print("Modified matrix:", matrix)

# Array dimensions
print("Number of rows:", len(matrix))                 # 3
print("Number of columns:", len(matrix[0]))           # 3
print("Total elements:", len(matrix) * len(matrix[0])) # 9

# Iterating through 2D array
print("\nMatrix elements:")
for i in range(len(matrix)):          # Loop through rows
    for j in range(len(matrix[i])):   # Loop through columns
        print(f"matrix[{i}][{j}] = {matrix[i][j]}", end="  ")
    print()  # New line after each row

# Using nested loops (alternative)
print("\nUsing nested for-each loops:")
for row in matrix:
    for element in row:
        print(element, end=" ")
    print()

# Adding rows and columns
matrix.append([10, 11, 12])          # Add new row
print("\nAfter adding row:", matrix)

# Add column to each row
for row in matrix:
    row.append(0)                    # Add 0 to each row
print("After adding column:", matrix)

# Slicing 2D arrays
print("\nFirst two rows:", matrix[:2])           # Rows 0-1
print("First two columns of first row:", matrix[0][:2])  # Columns 0-1 of row 0

# Transpose a matrix (using list comprehension)
transpose = [[matrix[j][i] for j in range(len(matrix))] 
             for i in range(len(matrix[0]))]
print("\nTranspose of matrix:")
for row in transpose:
    print(row)

# Real-world example: Student marks
student_marks = [
    ["Alice", 85, 92, 78],
    ["Bob", 72, 88, 91],
    ["Charlie", 95, 89, 94]
]

print("\nStudent Marks Table:")
print("Name\tMath\tScience\tEnglish")
print("-" * 35)
for student in student_marks:
    print(f"{student[0]}\t{student[1]}\t{student[2]}\t{student[3]}")
Important:
  • 2D arrays are lists of lists - each inner list represents a row
  • Access elements using array[row][column]
  • All rows should have the same number of columns for a proper matrix
  • Use nested loops to iterate through 2D arrays
  • For mathematical operations, use NumPy arrays instead
NumPy

NumPy Arrays for Scientific Computing

NumPy (Numerical Python) is the fundamental package for scientific computing in Python. NumPy arrays are faster, more memory efficient, and provide extensive mathematical operations compared to Python lists.

# Import NumPy
import numpy as np

# Creating NumPy arrays
arr = np.array([1, 2, 3, 4, 5])
matrix = np.array([[1, 2], [3, 4]])

# Array attributes
shape = arr.shape
dtype = arr.dtype
NumPy Array Examples
import numpy as np

# Creating NumPy arrays
arr1d = np.array([1, 2, 3, 4, 5])           # 1D array
arr2d = np.array([[1, 2, 3], [4, 5, 6]])    # 2D array
arr3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])  # 3D array

print("1D Array:", arr1d)
print("2D Array:\n", arr2d)
print("3D Array:\n", arr3d)

# Array attributes
print("\nArray Attributes:")
print("Shape of arr1d:", arr1d.shape)        # (5,)
print("Shape of arr2d:", arr2d.shape)        # (2, 3)
print("Shape of arr3d:", arr3d.shape)        # (2, 2, 2)
print("Dimensions of arr2d:", arr2d.ndim)    # 2
print("Data type:", arr1d.dtype)             # int64
print("Size (total elements):", arr2d.size)  # 6

# Special arrays
zeros = np.zeros((3, 4))                     # 3x4 matrix of zeros
ones = np.ones((2, 3))                       # 2x3 matrix of ones
identity = np.eye(3)                         # 3x3 identity matrix
range_arr = np.arange(0, 10, 2)              # [0, 2, 4, 6, 8]
linspace = np.linspace(0, 1, 5)              # 5 evenly spaced numbers between 0 and 1

print("\nSpecial Arrays:")
print("Zeros matrix:\n", zeros)
print("Ones matrix:\n", ones)
print("Identity matrix:\n", identity)
print("Range array:", range_arr)
print("Linspace array:", linspace)

# Array operations
a = np.array([1, 2, 3, 4])
b = np.array([5, 6, 7, 8])

print("\nArray Operations:")
print("a + b =", a + b)           # [6, 8, 10, 12]
print("a - b =", a - b)           # [-4, -4, -4, -4]
print("a * b =", a * b)           # [5, 12, 21, 32] (element-wise)
print("a / b =", a / b)           # [0.2, 0.333, 0.428, 0.5]
print("a ** 2 =", a ** 2)         # [1, 4, 9, 16]
print("sin(a) =", np.sin(a))      # Trigonometric operations

# Matrix operations
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])

print("\nMatrix Operations:")
print("Matrix multiplication:\n", np.dot(matrix1, matrix2))
print("Transpose:\n", matrix1.T)
print("Determinant:", np.linalg.det(matrix1))
print("Inverse:\n", np.linalg.inv(matrix1))

# Array indexing and slicing
arr = np.array([[1, 2, 3, 4], 
                [5, 6, 7, 8], 
                [9, 10, 11, 12]])

print("\nArray Indexing:")
print("Element at [1, 2]:", arr[1, 2])        # 7
print("First row:", arr[0])                   # [1, 2, 3, 4]
print("First column:", arr[:, 0])             # [1, 5, 9]
print("Submatrix (rows 0-1, cols 1-3):\n", arr[0:2, 1:3])

# Boolean indexing
print("\nBoolean Indexing:")
print("Elements greater than 5:", arr[arr > 5])

# Statistical operations
data = np.array([23, 45, 67, 34, 89, 56, 72, 41])
print("\nStatistical Operations:")
print("Mean:", np.mean(data))
print("Median:", np.median(data))
print("Standard Deviation:", np.std(data))
print("Variance:", np.var(data))
print("Min:", np.min(data))
print("Max:", np.max(data))
print("Sum:", np.sum(data))

# Reshaping arrays
arr = np.arange(12)
print("\nOriginal array:", arr)
print("Reshaped to 3x4:\n", arr.reshape(3, 4))
print("Flattened:", arr.reshape(3, 4).flatten())
Why Use NumPy Arrays?
  • Performance: 10-100x faster than Python lists
  • Memory efficiency: Less memory consumption
  • Vectorized operations: Apply operations to entire arrays
  • Mathematical functions: Built-in linear algebra, statistics, etc.
  • Broadcasting: Operations on arrays of different shapes
  • Interoperability: Works with other scientific libraries
Methods

Array Methods and Operations

Python provides various methods and operations to manipulate arrays efficiently.

append() Method

Add element to the end of array

arr = [1, 2, 3]
arr.append(4)
# Result: [1, 2, 3, 4]
extend() Method

Add multiple elements from another array

arr1 = [1, 2, 3]
arr2 = [4, 5]
arr1.extend(arr2)
# Result: [1, 2, 3, 4, 5]
insert() Method

Insert element at specific position

arr = [1, 2, 4]
arr.insert(2, 3)
# Result: [1, 2, 3, 4]
remove() Method

Remove first occurrence of element

arr = [1, 2, 3, 2, 4]
arr.remove(2)
# Result: [1, 3, 2, 4]
Complete Array Methods Examples
# Creating sample array
numbers = [10, 20, 30, 40, 50]

print("Original array:", numbers)

# 1. append() - Add element to end
numbers.append(60)
print("After append(60):", numbers)

# 2. extend() - Add multiple elements
numbers.extend([70, 80, 90])
print("After extend([70, 80, 90]):", numbers)

# 3. insert() - Insert at position
numbers.insert(2, 25)  # Insert 25 at index 2
print("After insert(2, 25):", numbers)

# 4. remove() - Remove first occurrence
numbers.remove(40)
print("After remove(40):", numbers)

# 5. pop() - Remove and return element
popped = numbers.pop()      # Remove last
print(f"After pop(): {numbers}, popped: {popped}")

popped2 = numbers.pop(3)    # Remove at index 3
print(f"After pop(3): {numbers}, popped: {popped2}")

# 6. index() - Find index of element
idx = numbers.index(30)
print(f"Index of 30: {idx}")

# 7. count() - Count occurrences
count_array = [1, 2, 3, 2, 1, 2, 3, 2, 1]
count_2 = count_array.count(2)
print(f"Count of 2 in {count_array}: {count_2}")

# 8. sort() - Sort array
unsorted = [5, 2, 8, 1, 9, 3]
unsorted.sort()
print(f"Sorted array: {unsorted}")

unsorted.sort(reverse=True)
print(f"Sorted (descending): {unsorted}")

# 9. reverse() - Reverse array
arr = [1, 2, 3, 4, 5]
arr.reverse()
print(f"Reversed: {arr}")

# 10. copy() - Create shallow copy
original = [1, 2, 3]
copy_arr = original.copy()
copy_arr.append(4)
print(f"Original: {original}, Copy: {copy_arr}")

# 11. clear() - Remove all elements
arr_to_clear = [1, 2, 3, 4, 5]
arr_to_clear.clear()
print(f"After clear(): {arr_to_clear}")

# 12. List comprehension for filtering
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
evens = [x for x in numbers if x % 2 == 0]
squares = [x**2 for x in numbers]
print(f"Even numbers: {evens}")
print(f"Squares: {squares}")

# 13. map() and filter() functions
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, numbers))
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(f"Mapped squares: {squared}")
print(f"Filtered evens: {evens}")

Common Array Algorithms

Linear Search
def linear_search(arr, target):
    for i in range(len(arr)):
        if arr[i] == target:
            return i
    return -1
Bubble Sort
def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n-i-1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
Sum of Array
def array_sum(arr):
    total = 0
    for num in arr:
        total += num
    return total
Reverse Array
def reverse_array(arr):
    left, right = 0, len(arr)-1
    while left < right:
        arr[left], arr[right] = arr[right], arr[left]
        left += 1
        right -= 1

Array Types Comparison

Aspect Python Lists array Module NumPy Arrays
Data Types Heterogeneous (any type) Homogeneous (single type) Homogeneous (single type)
Performance Slow for numerical operations Faster than lists Fastest (C implementation)
Memory Usage High (stores references) Lower (stores raw values) Lowest (contiguous memory)
Operations Basic list operations Basic array operations Advanced math operations
Multi-dimensional Lists of lists 1D only N-dimensional
Use Case General purpose, mixed data Simple homogeneous data Scientific computing, ML

Real-World Applications

Data Analysis

Analyzing sales data with NumPy:

import numpy as np
sales = np.array([15000, 22000, 18000, 
                  25000, 30000, 28000])
print(f"Average: ${np.mean(sales):,.2f}")
print(f"Max: ${np.max(sales):,.2f}")
print(f"Growth: {sales[1:]/sales[:-1] - 1}")
Image Processing

Images as 3D arrays (height×width×RGB):

import numpy as np
# Create 100x100 RGB image
image = np.zeros((100, 100, 3), dtype=np.uint8)
image[30:70, 30:70] = [255, 0, 0]  # Red square
# Image now has a red square in center
Game Boards

Tic-Tac-Toe board as 2D array:

board = [[' ', ' ', ' '],
         [' ', ' ', ' '],
         [' ', ' ', ' ']]

def display_board(board):
    for row in board:
        print('|'.join(row))
        print('-' * 5)
Student Database

Storing student records in 2D array:

students = [
    ["ID", "Name", "Grade", "GPA"],
    ["S001", "Alice", "A", 3.8],
    ["S002", "Bob", "B", 3.2],
    ["S003", "Charlie", "A", 3.9]
]
for student in students:
    print(f"{student[0]:5} {student[1]:10} "
          f"{student[2]:5} {student[3]:5}")

Practice Exercises

Array Practice Exercises
# Exercise 1: Find Maximum and Minimum
# Write a function to find max and min in array

# Exercise 2: Matrix Addition
# Add two matrices (2D arrays) element-wise

# Exercise 3: Array Rotation
# Rotate array by k positions
# Example: [1,2,3,4,5], k=2 → [4,5,1,2,3]

# Exercise 4: Remove Duplicates
# Remove duplicate elements from array

# Exercise 5: Pascal's Triangle
# Generate first n rows of Pascal's triangle

# Exercise 6: Spiral Matrix
# Print elements of matrix in spiral order

# Exercise 7: Frequency Counter
# Count frequency of each element in array

# Exercise 8: Matrix Multiplication
# Multiply two matrices (nested loops)

# Exercise 9: Second Largest
# Find second largest element in array

# Exercise 10: Array Intersection
# Find common elements between two arrays

Key Takeaways

  • Python lists serve as dynamic arrays but can hold mixed data types
  • 1D arrays are linear collections accessed by single index [0, 1, 2, ...]
  • 2D arrays are arrays of arrays, representing matrices/tables
  • NumPy arrays are essential for numerical computing and data science
  • Array indices start at 0 in Python (zero-based indexing)
  • Slicing syntax: array[start:stop:step] creates subarrays
  • Use append(), extend(), insert() to add elements
  • Use remove(), pop() to delete elements
  • sort() and reverse() modify arrays in-place
  • List comprehensions provide concise way to transform arrays
  • For mathematical operations on large datasets, always use NumPy
  • 2D arrays should have consistent row lengths for proper matrix operations
Next Topics: Learn about Python Functions - creating reusable code blocks, parameters, return values, lambda functions, and decorators.