Defining Dimensions for Arrays
Course: DSA › Arrays › Multidimensional Arrays
Now that we understand why a single-dimensional array isn’t enough for structured data like “classes and students,” let’s look at how we actually define a multidimensional array — what “dimensions” means, how they map to rows and columns, and how you use them in code.
What Is a Dimension?
Think of a dimension as an axis of organisation.
- A 1D array has one axis: position along a line. You need one index to pinpoint an element.
- A 2D array has two axes: rows and columns. You need two indices to pinpoint an element.
The moment you add a second axis, you get a grid — and a grid is exactly the right structure for data that has a natural “rows and columns” shape: classes and students, pixels on a screen, cells in a spreadsheet, entries in a matrix.
---
config:
theme: base
themeVariables:
primaryColor: "#dbeafe"
primaryBorderColor: "#3b82f6"
primaryTextColor: "#1e3a5f"
lineColor: "#64748b"
secondaryColor: "#ede9fe"
tertiaryColor: "#fef9c3"
---
flowchart LR
subgraph one["1D Array — 1 axis"]
a0["0"] --- a1["1"] --- a2["2"] --- a3["3"] --- a4["4"]
end
subgraph two["2D Array — 2 axes"]
direction TB
r0c0["[0][0]"] --- r0c1["[0][1]"] --- r0c2["[0][2]"]
r1c0["[1][0]"] --- r1c1["[1][1]"] --- r1c2["[1][2]"]
r2c0["[2][0]"] --- r2c1["[2][1]"] --- r2c2["[2][2]"]
end
1D array needs one index. 2D array needs two indices — one for the row, one for the column.
Rows and Columns
A 2D array is defined by two numbers:
- Number of rows — how many groups (e.g. classes)
- Number of columns — how many items per group (e.g. students per class)
---
config:
theme: base
themeVariables:
primaryColor: "#dbeafe"
primaryBorderColor: "#3b82f6"
primaryTextColor: "#1e3a5f"
lineColor: "#64748b"
secondaryColor: "#ede9fe"
tertiaryColor: "#fef9c3"
---
flowchart LR
label["rows × columns"] --> grid
subgraph grid["4 × 5 grid"]
direction TB
r0["[0][0] │ [0][1] │ [0][2] │ [0][3] │ [0][4]"]
r1["[1][0] │ [1][1] │ [1][2] │ [1][3] │ [1][4]"]
r2["[2][0] │ [2][1] │ [2][2] │ [2][3] │ [2][4]"]
r3["[3][0] │ [3][1] │ [3][2] │ [3][3] │ [3][4]"]
end
A 2D array with 4 rows and 5 columns. Total elements = 4 × 5 = 20.
The total number of elements in a 2D array is always:
Total elements = number of rows × number of columns
Solving the School Classes Problem
Remember the school with 4 classes and 60 students each? Instead of 4 separate arrays, we define a single 2D array:
---
config:
theme: base
themeVariables:
primaryColor: "#dbeafe"
primaryBorderColor: "#3b82f6"
primaryTextColor: "#1e3a5f"
lineColor: "#64748b"
secondaryColor: "#ede9fe"
tertiaryColor: "#fef9c3"
---
flowchart LR
label["ages\n4 × 60"] --> g
subgraph g["ages — 4 rows (classes), 60 columns (students)"]
direction TB
r0["class 0 → ages[0][0] ages[0][1] ages[0][2] ... ages[0][59]"]
r1["class 1 → ages[1][0] ages[1][1] ages[1][2] ... ages[1][59]"]
r2["class 2 → ages[2][0] ages[2][1] ages[2][2] ... ages[2][59]"]
r3["class 3 → ages[3][0] ages[3][1] ages[3][2] ... ages[3][59]"]
end
A 4 × 60 two-dimensional array replacing four separate arrays.
One name. One structure. All the data — and accessing the age of student 12 in class 2 is just ages[2][12].
Accessing Elements — Two Indices
With a 2D array, every element needs exactly two indices:
array[row_index][column_index]
- First index → which row (which class)
- Second index → which column (which student in that class)
---
config:
theme: base
themeVariables:
primaryColor: "#dbeafe"
primaryBorderColor: "#3b82f6"
primaryTextColor: "#1e3a5f"
lineColor: "#64748b"
secondaryColor: "#ede9fe"
tertiaryColor: "#fef9c3"
---
flowchart LR
idx["ages[2][3]"] --> row["row = 2<br/>(class 2)"]
idx --> col["col = 3<br/>(4th student)"]
row --> cell["value at<br/>row 2, col 3"]
col --> cell
Two indices locate a single element: first selects the row, second selects the column.
Think of it like a map coordinate. Row first, column second — just like “row 3, seat 5” in a cinema. The row gets you to the right group, and the column gets you to the right item within that group.
Declaring a 2D Array in Python
from typing import List
# Declare a 2D array: 4 classes, 5 students each (using 5 for demo)
rows: int = 4
cols: int = 5
# Method 1: list comprehension (correct)
ages: List[List[int]] = [[0] * cols for _ in range(rows)]
# Assign some values
ages[0][0] = 6
ages[1][2] = 8
ages[3][4] = 10
# Access a specific element
print("Class 1, Student 3:", ages[1][2]) # → 8
# Print all rows
for row_index in range(rows):
print(f"Class {row_index}:", ages[row_index])
Common trap — don’t use
[[0] * cols] * rows!This looks like it creates 4 independent rows, but it actually creates 4 references to the same row. Editing
ages[0][1]would change every row. Always use the list comprehension form:[[0] * cols for _ in range(rows)].
Key Takeaway
| Concept | Detail |
|---|---|
| Dimension | An axis of organisation — 1D = line, 2D = grid |
| 2D array size | Defined as rows × columns |
| Access syntax | array[row][col] — row first, column second |
| Total elements | rows × columns |
| Benefit over 1D | Naturally represents tabular / grouped data |
When your data has two levels of grouping (classes → students, rows → columns, X → Y), define those two levels as the two dimensions of a 2D array.