14-2d-arrays.py
python-programming / arrays  ·  Lesson 15
Lesson 15 — Arrays

2-D Arrays

Represent rows and columns with a list of lists.

A 2-D array stores data in rows and columns. In Python it can be represented as a list containing other lists.

Predict

Read the code first. Do not run it yet.

2d-arrays-predict.py
grid = [[2, 4, 6],
        [8, 10, 12]]
print(grid[0][1])
print(grid[1][2])
Predict before running
  • Predict both outputs.
  • Which index chooses the row?
  • Which index chooses the column?

Investigate

Use this code to explain exactly how the program works.

2d-arrays-investigate.py
scores = [[12, 15, 18],
          [20, 17, 16]]
for row in range(2):
    for column in range(3):
        print(scores[row][column])
Investigate the code
  • How many values are printed?
  • Which loop moves through rows?
  • Which loop moves through columns?

Rows and columns

table.py
marks = [[68, 71, 75],
         [82, 79, 88],
         [55, 60, 58]]
print(marks[1][2])
marks[2][0] = 57
print(marks[2])
Output
88
[57, 60, 58]
How it works
  • marks[1] selects the second row.
  • marks[1][2] selects the third value in that row.
  • Nested loops are useful for processing every cell.
Static 2-D arrays and tablesWhen a Python list of lists is being used as a 2-D array, keep the number of rows and columns fixed. A 2-D array can model a table: each row can represent one record and each column can represent a field. Records are developed in a later lesson.

Self-study guide

A two-dimensional array can be pictured as rows and columns. OCR requires practical use of 2-D arrays and specifically expects them to be usable for table-like data. Python commonly represents this with a list of lists.

The two languages use different indexing syntax. OCR ERL writes a two-dimensional position like gameboard[1,0]; Python normally writes gameboard[1][0].

OCR focusUse 2-D arrays practically, treat them as fixed structures, and understand row/column indexing. Be able to translate OCR two-index notation into Python nested indexing.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
array gameboard[8,8]gameboard = [[...], [...], ...]OCR declares dimensions directly; Python uses nested lists.
gameboard[1,0]gameboard[1][0]Row index first, then column index.
fixed 2-D arrayfixed-size list of lists for these tasksDo not change the number of rows/columns during OCR array practice.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

Nested loops are a natural way to process every cell.

lesson-15-worked.py
scores = [
    [4, 6, 5],
    [7, 3, 8],
    [9, 6, 2]
]

for row in scores:
    row_total = 0

    for value in row:
        row_total = row_total + value

    print(row_total)
Output
15
18
17
Work through it line by line
  • The outer loop selects one row at a time.
  • row_total is reset at the start of each new row.
  • The inner loop processes every value in that selected row.
  • The total is printed after the inner loop finishes.

Another example

You can access one specific cell using two indexes.

lesson-15-example-2.py
grid = [
    [4, 7, 2],
    [9, 1, 6]
]

print(grid[0][1])
grid[1][2] = 10
print(grid)
Output
7
[[4, 7, 2], [9, 1, 10]]
Why this example matters
  • grid[0] selects the first row.
  • The following [1] selects the second item within that row.
  • The update changes one cell without resizing the table.

Common mistakes

Avoid these errors
  • Reversing row and column indexes.
  • Using OCR grid[1,0] directly in ordinary Python list-of-lists code.
  • Resetting counters/totals at the wrong level of a nested loop.
Exam-safe warningOCR ERL uses array[row,column]; Python list-of-lists code normally uses array[row][column].

Exam-ready checklist

You should now be able to
  • Access and change individual 2-D array cells.
  • Use nested loops to process rows and columns.
  • Translate between OCR two-index notation and Python nested indexing.
  • Keep the 2-D structure fixed during array tasks.

What you have learned

  • A 2-D array has rows and columns.
  • Python represents it using a list of lists.
  • Use two indices: row first, then column.
  • Nested loops can process every item.