GCSE · Computer Science · Edexcel · Spec 1CP2

Two-dimensional arrays

Noughts and crosses, a seating plan, a spreadsheet: all tables, and Python can keep a whole one in one variable. The trick is asking for exactly the cell you want.

Computer Science · 2D lists

Which cell does it reach?

Step through a short program on a table of names and scores. Say the row and column before you press Next.

Before you press Next: which row, and which column, will this line reach?

1 # col 0 col 1 col 2 col 3 col 4
2 # row 0 "Ava" "Ben" "Cat" "Dev" "Eli"
3 # row 1 12 30 25 18 41
4 scores = [["Ava", "Ben", "Cat", "Dev", "Eli"],
5 [12, 30, 25, 18, 41]]
6 print(scores[0])
7 print(scores[1])
8 print(scores[0][2])
9 print(scores[1][0])
10 scores[1][0] = 50
11 print(scores)
12 scores.append([10, 25, 40, 33, 49])
13 print(scores)

Variables

scores=2 rows, 5 columnsreaches=nothing yetrow index=-column index=-

Output

 

Step 1: Here is the whole table stored in ONE variable. Row 0 holds the names, row 1 holds the scores. The grey comment lines are just a picture of it, with the index numbers along the top and side.

1 / 9

Predict first, then step. Every value shown is written into the lesson; the page does not run the code.

Step 1 of 9: Here is the whole table stored in ONE variable. Row 0 holds the names, row 1 holds the scores. The grey comment lines are just a picture of it, with the index numbers along the top and side..

Exam line: Row index first, then column index, both starting from 0 - so scores[1][0] is the first item of the second row.
Watch out: Writing the column index first, or counting from 1, quietly picks the wrong cell.

Array or list?

Array (static)vsList (dynamic)

Same square brackets in Python, different idea underneath.

Focus

Type of data structure

Array (static)

Static

List (dynamic)

Dynamic

The insight

Static means the size is fixed. Dynamic means the size can change. Everything else on this page follows from that.

Size

Array (static)

Fixed

List (dynamic)

Can change

Adding and deleting items

Array (static)

Items cannot be added or deleted (the contents can still be changed)

List (dynamic)

Items can be added and deleted

Data types of the items

Array (static)

All the same data type

List (dynamic)

Can be different data types

In Python

Array (static)

Written with square-bracket list syntax

List (dynamic)

Written with square-bracket list syntax

Computer Science · Reading code

What does this line do?

Pick a line, then pick what it does.

Still to sort

Returns a whole row (0)

One index in brackets.

Where the line is: Only a row index is given, so a whole list comes back. Two indexes would return a single item.

Returns a single item (0)

Row index, then column index. No = sign.

Where the line is: Two indexes but no assignment: it reads one item and changes nothing.

Replaces an item (0)

Both indexes, then = and a new value.

Where the line is: It has the same two indexes as returning an item, but the = and new value overwrite that cell.

Adds a new row (0)

append() with a whole row inside.

Where the line is: No index picks a cell here: append() puts a whole new row at the end.

8 of 8 still to sort.

marks is a 2D list with 3 rows and 4 columns. Sort each line by what it does.

Exam line: No index = adds a row with append(). One index = whole row. Two indexes = one item. Two indexes with = replaces an item.
Watch out: Two lines can look nearly the same. The only difference between returning an item and replacing it is the = and the new value.

Computer Science · Loops

Total and average of one row

Back to the original scores table (5 names in row 0, 5 scores in row 1). Build the loop that finds the total and average of the scores in row 1. Fill each gap.

  1. total = 0Start the running total at zero before the loop.
  2. missing step
Which line is step 2?

Exam line: Initialise total to 0, add each value of the chosen row in a loop, then divide the total by the number of values - with the row index in every reference to the row.

Computer Science · Game boards

The X in the wrong place

A noughts-and-crosses board is a list of three rows. The player chooses column 2 and row 0. Find the one line that puts the X in the wrong cell.

A student's answer — which line goes wrong?

Exam line: In a game board, the row index goes first: board[y][x] when y is the row and x is the column.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Store a whole table in one variable, then pick out any row or cell you like.

What you need to know

  • An array is static: one name, many items, fixed size, all items one data type. Items can change, but not be added or deleted.
  • A list is dynamic: its size can change, items can be added and deleted, and items can be different data types.
  • A 2D array or list holds a list as each element, so it reads like a table of rows and columns.
  • Row and column index numbers both start from zero, so the first row is row 0 and the first column is column 0.
  • Have a goYour classmate Sam says a table with 5 columns has a last column numbered 5. Is Sam right?

    No. The last column is column 4.

    Counting starts at zero, so five columns are numbered 0, 1, 2, 3, 4.

  • In Python a 2D list is a list of lists: brackets round each row, another pair round all the rows, commas between rows.
  • One index in brackets returns a whole row: scores[0] is the first row.
  • Two indexes return one item, row first: scores[1][0] is the first item of the second row.
  • Have a goRow 0 of scores holds names. Write the expression that returns the fourth name.

    scores[0][3]

    Row index first (0, the names row), then column 3, because the first column is 0 so the fourth is 3.

  • Replace one item with list_name[row][column] = new_value. Add a whole new row at the end with append().
  • Have a goscores has 2 rows. After scores.append([1, 2, 3, 4, 5]), how many rows is that, and what is the new row's index?

    3 rows; the new row has index 2.

    append() adds the new row at the end, and row indexes keep counting from zero: 0, 1, 2.

  • To total or average one row, loop through it with the row index included, then divide the total by the number of values.
  • Print a game board row by row with a loop through each row and, inside it, a loop through each column.

The big picture

A 2D list is a list of lists that you read like a table. The first index picks the row, the second picks the column, and both count from zero. With those two indexes you can return a row, return or replace an item, add a row with append(), total a row in a loop and print a game board.

Key points

1A 2D list is a list of lists: each row is itself a list, and the whole thing can be pictured as a table.
2Row index first, then column index, both starting from zero: scores[1][0] is the first item of the second row.
3One index returns a whole row; two indexes return or replace a single item; append() adds a new row at the end.
4To total or average one row, loop through it with the row index included. To print a board, loop through each row and each column.

Worked example

Problem

A 3 x 3 noughts-and-crosses board is stored as a 2D list, with "-" meaning an empty cell. Print it row by row as a grid.

⚠ Watch out

Writing the column index first (for example board[x][y] when y is the row), or counting rows and columns from 1. Either one quietly picks the wrong cell. Say it as row, then column, starting at 0.

🧠

Memory hook

Open the big box first (the row), then pick the item inside it (the column) - and the first box is always numbered 0.

✓

Check yourself

seats is a 2D list with 3 rows and 4 columns. Write the expression for the last row, last column. Check: seats[2][3] (row first, counting from 0).

Flashcards

(12)
What is an array?
A static data structure that holds multiple items under one name. It has a fixed size and its items are all the same data type. The contents can be changed, but items cannot be added or deleted.
What is a list?
A dynamic data structure that holds multiple items under one name. Its size can change, so items can be added and deleted, and it can hold items of different data types.
Static or dynamic: which is an array and which is a list?
An array is static (fixed size). A list is dynamic (its size can change).
What is a two-dimensional array or list?
One with both rows and columns of values. Each element is itself a list or array, so several arrays or lists live in one variable. Picture it as a table.
Where do row and column index numbers start?
At zero, for both rows and columns.
How do you write a 2D list in Python?
As a list of lists: a set of square brackets round each row, plus a set round all the rows, with a comma between each row.
What does one index in square brackets return from a 2D list?
A whole row. If scores is a 2D list, scores[0] returns the whole first row.
Which comes first in a 2D list, the row index or the column index?
The row index comes first, then the column index.
How do you replace one item in a 2D list?
Give both indexes and assign a new value: list_name[row][column] = new_value
How do you add a new row to a 2D list?
Use append() with the new row inside it, for example scores.append([10, 25, 40, 33, 49]). The row is added at the end.
How do you find the average of one row of a 2D list?
Loop through that row (row index included), adding each value to a total that starts at 0. Then divide the total by the number of values.
How do you print a game board stored as a 2D list in grid form?
Loop through each row, and for each row loop through each column. The board prints row by row.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More Edexcel GCSE Computer Science topics

See the full Edexcel Computer Science curriculum →

How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 21 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.