GCSE · Computer Science · OCR · Spec J277

2D arrays

"Row F, seat 12" finds one cinema seat. A 2D list works the same way, except both numbers start at 0. Swap them and you're in the wrong seat.

Computer Science · 2D lists

A table inside one variable

Three players, three rounds. Each row of scores is one player's scores, and each column is one round. Step through and watch what each line reaches.

Before you press Next twice: which number do you think scores[1][2] picks out?

1 scores = [[7, 5, 9],
2 [4, 8, 6],
3 [3, 10, 2]]
4 print(scores[1])
5 print(scores[1][2])
6 scores[2][0] = 11
7 scores.append([6, 9, 4])
8 print(scores[3])

Variables

scores[0]=[7, 5, 9]scores[1]=[4, 8, 6]scores[2]=[3, 10, 2]

Output

 

Step 1: One variable, three lists inside it. That's a table: 3 rows, 3 columns. The top row is row 0 and the left column is column 0. Both start from 0.

1 / 6

Each chip is one row of the table. The output box shows what each print line gives.

Step 1 of 6: One variable, three lists inside it. That's a table: 3 rows, 3 columns. The top row is row 0 and the left column is column 0. Both start from 0..

Watch out: Row first, then column. scores[1][2] and scores[2][1] are different seats.

Computer Science · Index order

Where's the 8?

marks = [[14, 9, 17], [11, 20, 8]]. That's 2 rows and 3 columns, and the 8 is the bottom-right value. Four classmates each write how to reach it.

Which one is closest to what you'd have written?
How sure are you?

Computer Science · Reading the syntax

What does each line do?

Sort each line by what it does to temps.

Still to sort

Gets a whole row (0)

One index in square brackets.

Where the line is: Only one set of square brackets after temps, so you get the full row, not one value.

Gets a single item (0)

Row index, then column index, and the value is only read.

Where the line is: Two indexes but nothing assigned to that location: the table doesn't change.

Replaces an item (0)

Both indexes on the left of =.

Where the line is: The location named on the left of = gets a new value. Two indexes on the right side are only being read.

Adds a new row (0)

append() with the new row given as a list.

Where the line is: The table gains a row at the end. Nothing already in it changes.

8 of 8 still to sort.

temps is a 2D list with 3 rows and 4 columns. Count the indexes, then check which side of the = they're on.

Computer Science · Check the indexes

The fix that fixed the wrong thing

scores has one row per player and one column per round. Player 1's score for round 0 was mistyped and should be 10. Which line changes the wrong value?

A classmate's program — which line goes wrong?

Computer Science · Looping through a row

Average of one row

scores = [[6, 8, 5, 9], [7, 4, 10, 3]]. Fill in the missing lines so the program works out the average of row 1.

  1. row = 1Row 1 is the second row: [7, 4, 10, 3].
  2. missing step
Which line is step 2?

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

One variable, a whole table of values. Every value has a two-number address: row first, then column, and both start at 0.

What you need to know

  • A 2D array or list stores several arrays or lists in one variable.
  • Two-dimensional just means it has both rows and columns of values, so picture a table.
  • Every row and every column has an index number, and both start from 0.
  • One index in square brackets, like scores[1], gives you a whole row.
  • Have a got = [[2, 4], [6, 8], [1, 3]]. What does t[2] give you?

    [1, 3]

    One index picks a whole row, and counting starts at 0, so index 2 is the third row. [6, 8] is what you get by counting from 1.

  • Two indexes give you a single item: row index first, then column index, as in scores[1][2].
  • Have a goSame t. Your friend bets their last biscuit that t[1][0] is 4, "because you go across 1, then down 0, like on a graph". Is the biscuit safe?

    No: t[1][0] is 6. Enjoy the biscuit.

    The row index comes first, so row 1 is [6, 8] and column 0 of it is 6. Reading it like x, y coordinates gives 4, which is really t[0][1].

  • To replace an item, point at it with both indexes and assign the new value: scores[2][0] = 11.
  • append() adds a new row to the end of a 2D list, and the row goes in as a list.
  • That's a list job: a fixed-size array can't gain items, so it can't gain a row.
  • Totalling a row uses the 1D loop with one tweak: add the row index, so each value is scores[row][i].
  • Have a goYou want to total row 0 of t. Inside the loop, which goes in the brackets: t[0][i] or t[i][0]?

    t[0][i]

    The row index stays fixed at 0 while the column index i moves along the row. t[i][0] fixes the column and changes the row, so it isn't adding row 0's values.

  • Start the total at 0, add each value in the row, then divide by how many values there are.

The big picture

A two-dimensional (2D) array or list stores several arrays or lists in one variable: picture a table of rows and columns, both indexed from 0. One index, scores[1], gives a whole row; a row index then a column index, scores[1][2], gives one item. Assign to both indexes to replace an item. append() adds a new row, given as a list, to the end of a 2D list. To total or average one row, use the 1D loop with one adjustment: each value is scores[row][i].

Key points

1A 2D array or list stores several arrays or lists in one variable: a table of rows and columns.
2Rows and columns are both indexed from 0.
3scores[r] gives a whole row; scores[r][c] gives a single item, row index first.
4Replace an item by naming it with both indexes and assigning a new value; append() adds a new row, given as a list, to the end of a 2D list.
5To total or average one row, loop through it with column index i, adding scores[row][i] to a total that starts at 0, then divide by the number of values.

Worked example

Problem

grid = [[2, 4, 6], [1, 3, 5]]. A program then runs grid[0][2] = grid[1][0] + 7, then grid.append([9, 8, 7]), then print(grid[2][1]) and print(grid[0]). What gets printed, and what does grid hold at the end?

⚠ Watch out

Putting the column index first, as if it were x, y coordinates. In scores[r][c] the first index is the row. Swap them and you read, or overwrite, a different value, often with no error to warn you.

🧠

Memory hook

Cinema seats: pick the row, then the seat. Row first, column second, and the front row is row 0.

✓

Check yourself

Cover the page. For m = [[5, 1, 7], [2, 9, 4]], write the expression for the 9, a line changing the 7 to 0, and a line adding the row [3, 3, 3].

Flashcards

(12)
What is a two-dimensional (2D) array or list?
Several arrays or lists stored in one variable. You can picture it as a table of rows and columns.
What does "two-dimensional" describe?
A list or array that has both columns and rows of values.
In a 2D list, what number do the row and column indexes start from?
0, for both rows and columns. The top-left value is at [0][0].
In scores[a][b], which index is the row and which is the column?
a is the row index, b is the column index. Row first, then column.
What part of the table does scores[2] give you?
The whole of row 2, the third row. One index in square brackets picks a row.
How do you get a single item from a 2D list?
The row index in square brackets, followed by the column index in square brackets, e.g. scores[2][0].
How do you replace one item in a 2D list?
Use both indexes to identify the value, then assign the new value to that location, e.g. scores[0][1] = 12.
How do you add a new row to the end of a 2D list?
Use append() with the new row given as a list, e.g. scores.append([5, 7, 3]).
Why does appending a row apply to a 2D list rather than a fixed-size array?
A static, fixed-size array can't gain items. A 2D list can have a new row added to its end.
A 2D list has 5 rows. What is the index of the last row?
4. The rows are numbered 0 to 4.
Looping through one row of a 2D list: what's the one adjustment from the 1D technique?
Add the index of the row of interest, so each value is scores[row][i] instead of scores[i].
What's the method for the total and average of one row?
Set total to 0, loop through the row with column index i adding scores[row][i], then divide the total by the number of values.

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 OCR GCSE Computer Science topics

See the full OCR Computer Science curriculum →

How this lesson was checked. This OCR GCSE Computer Science (specification J277)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 3 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.