A two-dimensional (2D) array is a grid of values arranged in rows and columns, stored under a single variable name. Each element is accessed using two index values — one for the row and one for the column — making 2D arrays ideal for grids, tables, game boards, and matrices.

What is a 2D array and how does it relate to a 1D array?

A one-dimensional (1D) array is a single row of values: [10, 20, 30, 40, 50]. A two-dimensional array extends this to a grid — a list of lists, where each inner list is one row:

       Col 0  Col 1  Col 2
Row 0 [  10,   20,   30 ]
Row 1 [  40,   50,   60 ]
Row 2 [  70,   80,   90 ]

This is a 3×3 grid with 3 rows and 3 columns. Element at row 1, column 2 is 60.

A real-world analogy: a spreadsheet is a 2D array. Row and column together identify any cell uniquely.

How do you declare a 2D array in Python?

In Python, a 2D array is implemented as a list of lists:

# A 3x3 grid of scores
grid = [
    [10, 20, 30],   # row 0
    [40, 50, 60],   # row 1
    [70, 80, 90]    # row 2
]

Each inner list is a row. The outer list holds all the rows. You can create one directly (as above) or build it programmatically:

# Create a 3x4 grid filled with zeros
rows = 3
cols = 4
grid = [[0] * cols for _ in range(rows)]
print(grid)   # [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

Note: avoid [[0] * cols] * rows — this creates multiple references to the same inner list, causing unexpected behaviour when you modify one row.

How do you access and modify individual elements?

Use two indices: grid[row][col]:

grid = [
    [10, 20, 30],
    [40, 50, 60],
    [70, 80, 90]
]

# Read
print(grid[0][0])   # 10  (row 0, col 0)
print(grid[1][2])   # 60  (row 1, col 2)
print(grid[2][1])   # 80  (row 2, col 1)

# Write
grid[1][1] = 99
print(grid[1])      # [40, 99, 60]

The first index selects the row (the inner list); the second index selects the column (the element within that inner list).

How do you declare a 2D array in pseudocode?

Pseudocode (AQA style) uses:

DECLARE grid : ARRAY[0:2, 0:2] OF INTEGER

grid[0,0] ← 10
grid[0,1] ← 20
grid[0,2] ← 30
grid[1,0] ← 40

Access syntax uses a comma to separate row and column indices: grid[row, col]. Python uses grid[row][col] instead. This difference appears in GCSE exam questions — make sure you use the correct notation for the context (pseudocode vs Python).

How do you iterate through all elements of a 2D array?

Use nested loops — an outer loop over rows, an inner loop over columns:

grid = [
    [10, 20, 30],
    [40, 50, 60],
    [70, 80, 90]
]

for row in range(3):        # rows 0, 1, 2
    for col in range(3):    # cols 0, 1, 2
        print(grid[row][col], end=" ")
    print()   # newline after each row

Output:

10 20 30 
40 50 60 
70 80 90 

Equivalent pseudocode:

FOR row ← 0 TO 2
    FOR col ← 0 TO 2
        OUTPUT grid[row, col]
    END FOR
    OUTPUT newline
END FOR

Nested loops processing a 2D array is one of the most common patterns in GCSE programming questions.

What are common real-world uses of 2D arrays?

Application How a 2D array models it
Noughts and crosses 3×3 grid: "X", "O", or "" in each cell
Seating plan Rows × columns, each cell holds a name or is empty
Battleships 10×10 grid: "ship", "hit", "miss", or "water"
Maze Grid of 0s (path) and 1s (wall)
Pixel images Each cell stores a colour value (grayscale or RGB)
Multiplication table table[i][j] = i * j for a times-table grid

A GCSE programming project that implements a board game, quiz grid, or grid-based simulation is a natural context for 2D arrays.

Frequently asked questions

How do I find the number of rows and columns in a 2D list in Python?

grid = [[1,2,3],[4,5,6],[7,8,9]]
rows = len(grid)           # 3
cols = len(grid[0])        # 3  (length of the first row)

len(grid) gives the number of rows (the length of the outer list). len(grid[0]) gives the number of columns (the length of any inner row — all rows must have the same length for a true rectangular 2D array).

Can a 2D array hold different data types in different cells?

In Python, yes — a list of lists can hold mixed types. However, for clarity and correctness at GCSE level, 2D arrays should hold a single consistent type (e.g. all integers or all strings). Mixed types make algorithms harder to reason about and are likely to cause TypeError exceptions in calculations. A 2D array representing a grid should contain only the data type appropriate for that grid's purpose.

What is the difference between a 2D array and a matrix?

A matrix is a mathematical concept — a rectangular grid of numbers supporting operations like addition, multiplication, and transposition. A 2D array is the programming data structure used to implement a matrix. In GCSE computing, the terms are sometimes used interchangeably. In mathematics, "matrix" implies specific algebraic rules; in programming, "2D array" only implies a grid structure with row/column indexing.

How do you copy a 2D list without the original and the copy sharing data?

Using copy() on the outer list gives a shallow copy — the outer list is new, but the inner lists are still shared. Modifying copy[0][0] modifies the original too. For a true independent copy, use copy.deepcopy():

import copy
original = [[1, 2], [3, 4]]
clone = copy.deepcopy(original)
clone[0][0] = 99
print(original[0][0])   # 1 — original unchanged

This is an advanced point rarely tested at GCSE, but understanding it prevents subtle bugs in programming projects.


Master 2D arrays and all GCSE data structures with worked examples and coaching from Professor Turing at aitutors.me.