Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Arrays

Every list in every app you have ever used is backed by an array. Your Spotify playlist, your Instagram feed, the leaderboard in your favourite game — all arrays under the hood. Understanding arrays means understanding the foundation that almost every other data structure is built on top of.

What is an array?

An array is a collection of items stored one after another in memory — like a row of labelled boxes on a shelf. Each box holds one value and has a unique number called an index starting from 0.

flowchart LR
    I["my_list"] --> A

    subgraph A["Array in memory"]
        direction LR
        B0["index 0\n---\n 'Alice' "]
        B1["index 1\n---\n 'Bob' "]
        B2["index 2\n---\n 'Carol' "]
        B3["index 3\n---\n 'Dan' "]
    end

Because items sit side-by-side in memory, the computer can jump straight to any slot using simple arithmetic. That is why reading arr[2] is just as fast as reading arr[0] — it is always a single step, no matter how large the array is.

A quick taste

players = ["Alice", "Bob", "Carol", "Dan"]

# Read any element in O(1) — instant, no matter the size
print("First player:", players[0])
print("Last player: ", players[-1])

# Iterate over all elements in O(n)
print("\nFull roster:")
for i, name in enumerate(players):
    print(f"  Slot {i}: {name}")

What you will learn in this section

This section builds your understanding layer by layer:

ChapterTopicThe big idea
RAMMemory and addressesWhy index access costs O(1)
Static ArraysFixed-size arraysFast reads, expensive inserts
Dynamic ArraysResizable arraysHow Python lists grow automatically
StacksLIFO structureOne of the most useful tools in CS
ProblemsPractice problemsApply everything you learned

Where arrays appear in the real world

  • Social media feeds — posts stored in order, newest first
  • Image pixels — a 1080p image is just an array of 2,073,600 colour values
  • Audio samples — a 44 kHz audio track stores 44,000 numbers per second
  • Undo history — every keystroke you type is pushed onto an array
  • Game inventories — item slots are fixed-size arrays
  • Spreadsheets — each row and column is an array of cells

The two flavours

Arrays come in two main varieties that this section explores in depth:

flowchart TD
    A["Arrays"] --> B["Static Arrays\n(fixed capacity)"]
    A --> C["Dynamic Arrays\n(auto-resize)"]
    B --> D["Great when size is known\nC arrays, pixel buffers"]
    C --> E["Great when size changes\nPython list, Java ArrayList"]

Start with RAM to understand why arrays are designed the way they are, then work through each chapter in order.