Python 2D lists

A two-dimensional list — a list of lists — is Python's standard way to represent grids and tables such as a noughts-and-crosses board, a seating plan, or a class register. Every UK A Level Computer Science specification requires 2D array handling, and it appears in higher-tier GCSE questions too. This unit covers building a 2D list, indexing rows and columns with grid[row][col], and iterating with nested for loops. Every example runs in the browser. Perfect for classroom teaching, GCSE and A Level revision, and independent or home-schooled learners.


What you'll learn

  • Build a 2D list as a list of lists
  • Access a cell with grid[row][col]
  • Iterate over all cells with nested for loops
  • Iterate over rows and columns separately
  • Store and update grid data (game boards, tables, seating plans)

UK GCSE topic coverage

  • 2D arrays
  • 2D indexing
  • 2D mutation
  • Nested iteration
  • Row algorithms
  • Column algorithms
  • 2D linear search
  • Constructing 2D lists
  • Applied 2D lists

Worked example

Print a 3x3 grid, row by row

board = [
    ["X", "O", "X"],
    ["O", "X", "O"],
    ["X", "O", "X"],
]
for row in board:
    for cell in row:
        print(cell, end=" ")
    print()

Try a full unit free with a RunPy account

Sign up free and get the whole of Unit 1 (Input, Output and Variables) with checked tasks and instant feedback, plus a selection of the exam-style questions so you can practice on real GCSE-style content before upgrading.


Lessons in this unit

Full interactive lessons with checked tasks are available to signed-in RunPy users. Unit 1 and a selection of exam-style questions are free with a RunPy account.

  1. What is a 2D list?
  2. Accessing elements
  3. Modifying elements
  4. Iterating a 2D list
  5. Row operations
  6. Column operations
  7. Searching a 2D list
  8. Building 2D lists from input
  9. Composite grid algorithm

Frequently asked questions

What is a 2D list in Python?
A list whose items are themselves lists — a list of rows. You access a cell with two indices: grid[row][col].
Which index comes first, row or column?
By convention: row first, column second. grid[2][0] is row 2, column 0.
Why nested for loops?
The outer loop moves through the rows, and the inner loop moves through the cells inside that row. Every cell in the grid gets visited exactly once.