Tutorial 17/20 — Advanced Logic

Python: 2D Lists

The Grid System. Lists within lists for complex mapping.

The Hook

Instead of a simple line of data, a 2D list is a GRID.

  • Spreadsheets (Rows & Columns)
  • Cinema Seats (Row A, Seat 5)
  • Game Maps (X, Y Coordinates)

We access data using two sets of brackets: grid[row][col].

Matrix Synthesis

# Cinema Seating Plan (2D)
seats = [
["A1", "A2", "A3"], # Row 0
["B1", "B2", "B3"], # Row 1
["C1", "C2", "C3"] # Row 2
]
# Accessing Row 1, Seat 1 (Middle)
print(seats[1][1]) # Prints "B2"

Cognitive Visualisation:

[0,0]
[0,1]
[0,2]
[1,0]
[1,1]
[1,2]
[2,0]
[2,1]
[2,2]

"Row First, then Column"

Logic Gate

Predict Output

A 2D list matrix looks like this:

matrix = [
  [15, 20],
  [18, 22]
]
print(matrix[1][0])
Level 46: Bronze

Grid Update

"The center spot "-" at Row 1, Column 1 needs to be updated. Change it to 'X'."

> matrix_write_standby
Level 47: Silver

Row Scanner

"A list contains multiple rows of data. Calculate the sum of the second row (Index 1)."

> scanner_module_active
Level 48: Gold

Matrix Loop

Coordinate Logic Engine

  • Loop through each row in the matrix.
  • Inside that, loop through each item in the row.
  • Print every item individually.
  • TIP: This is a Nested Loop (Loop-inside-a-loop).
> nested_vector_online