A table is a list of rows
After this: Make a list of rows, read one value with two positions, and count the rows and columns.
On this page · 5 sections · 3 checks
A teacher has two students, and each student has sat three tests. On paper this is a table: one row per student, one column per test.
| Test 1 | Test 2 | Test 3 | |
|---|---|---|---|
| Ana | 72 | 85 | 60 |
| Ben | 90 | 70 | 80 |
In "Lists" you kept one student's marks in a list. Two students would be two lists.
ana = [72, 85, 60]ben = [90, 70, 80]int[] ana = {72, 85, 60};int[] ben = {90, 70, 80};That works for two. With thirty students it is thirty names, and no loop can go through thirty separate lists. The fix is the same one that gave you lists in the first place: put the rows in one place.
A list whose values are lists
The values of a list can themselves be lists. So the table above can be one list with two values, and each value is a row. Data kept this way, a list inside a list, is called . The outer list holds the rows; each row holds the values.
To keep the numbers small, this tutorial uses a grid of six values instead of the marks.
grid = [[1, 2, 3], [4, 5, 6]]print(grid[1][2])6
int[][] grid = {{1, 2, 3}, {4, 5, 6}};System.out.println(grid[1][2]);6
Square brackets inside square brackets:Curly brackets inside curly brackets: the outer pair is the whole table, and each inner pair is one row. Read it as "a list of two things, and each thing is a list of three numbers".
Reading one cell
To read one value you give two positions. The first picks the row. The second picks the column inside that row. Both count from 0, as every position in a list does.
| column 0 | column 1 | column 2 | |
|---|---|---|---|
| row 0 | 1 | 2 | 3 |
| row 1 | 4 | 5 | 6 |
Work through grid[1][2] by hand:
grid[1]picks row 1, the second row:[4, 5, 6].[2]picks column 2 of that row, the third value: 6.
So grid[1][2] is 6. Read it left to right, one pair of brackets at a time: first the row, then the column. In both languages the spelling is the same, two pairs of square brackets.
A row is a list like any other. You can give it a name of its own and work with it as you did in "Lists": grid[1] is [4, 5, 6], and its first value is 4. The number of rows is the length of the outer list: len(grid)grid.length is 2. The number of columns is the length of one row: len(grid[0])grid[0].length is 3.
grid = [[1, 2, 3], [4, 5, 6]]print(len(grid))print(len(grid[0]))2 3
int[][] grid = {{1, 2, 3}, {4, 5, 6}};System.out.println(grid.length);System.out.println(grid[0].length);2 3
Step through the three reads. The box grid holds both rows. On line 35 one row is picked out and gets its own box.
- Run the programRun it and step through. The box grid holds both rows. Watch line 3 (Java: line 5) pick one row out and put it in its own box.
grid = [[1, 2, 3], [4, 5, 6]]print(grid[1][2])row = grid[1]print(row[0])print(len(grid))run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
A row in a loop
Once a row has a name, the loop from "Lists" goes through it value by value. Nothing about the row is new; what is new is only that it came out of a bigger list.
grid = [[1, 2, 3], [4, 5, 6]]row = grid[1]for value in row: print(value)print(len(row))run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
The box value takes 4, then 5, then 6: the three values of row 1. The length of the row, 3, is printed last.
Python runs in the browser when you press Run on the block below. Java does not run here: if you are learning Java, step through the traces above instead, and paste the code into a Java tool on your computer when you want to run it for real.
Change grid[1][2] to grid[0][0] and run again. Then add a third row and watch the last number change.
Check yourself
grid holds [[1, 2, 3], [4, 5, 6]]{{1, 2, 3}, {4, 5, 6}}. What is grid[1][2]?
What does this print?
grid = [[1, 2, 3], [4, 5, 6]]print(grid[0][2])print(len(grid))int[][] grid = {{1, 2, 3}, {4, 5, 6}};System.out.println(grid[0][2]);System.out.println(grid.length);grid holds [[1, 2, 3], [4, 5, 6]]{{1, 2, 3}, {4, 5, 6}}. Which of these reads the value 5?
Honesty notes
- Keep a table as a list whose values are rows, each row a list of its own: this is called nested data.
- Read one cell with two positions: the first picks the row, the second picks the column, and both start at 0.
- Ask for the number of rows with the length of the outer list, and the number of columns with the length of one row.
- In Java, write the fixed-size table as int[][] with two pairs of brackets; it reads with grid[row][col] like Python.