Choosing a shape for your data
After this: Pick the shape that fits a job, and build a table of rows that are really separate.
On this page · 5 sections · 3 checks
You now have three shapes: a flat list, a list of rows, and a dictionary of lists. Before you write a program, you choose one. The choice is about how you will reach a value, and getting it right makes the rest of the program short.
Three small cases
| The job | How you reach a value | The shape |
|---|---|---|
| The shopping for one trip: milk, eggs, bread | by position, or one at a time in a loop | a flat list |
| The temperature at each hour of each day of a week | by day number and hour number | a list of rows |
| Each student's marks, looked up by the student's name | by name, then by position | a dictionary of lists |
Ask two questions. Do the values come in groups? If not, a flat list is enough. If they do, are the groups numbered or named? Numbered groups fit a list of rows, because positions are numbers already. Named groups fit a dictionary of lists, because a dictionary looks up by name.
Shapes also nest the other way round. A list of dictionaries fits a collection of records, such as one dictionary per student with the keys "name" and "marks"; you reach a value with a position and then a key. The two-step reading you learnt here works for that shape too.
Building a table: the trap
A table of zeros is a common starting point, for a scoreboard or a game board. Python has a short way to write a row of three zeros, [0] * 3, and it is tempting to write the table as [[0] * 3] * 2, which reads as "that row, twice".In Java it is tempting to make one row, {0, 0, 0}, give it a name, and write the table as {row, row}: that row, twice.
Both make a table that looks right and is not. The outer list holds the same row twice, under two names, as b = a gave one list two names in "Lists". Step through it and watch what happens when one cell is changed.
row = [0, 0, 0]g = [row, row]g[0][0] = 9print(g)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 row changed, and both rows of g changed with it, on one line. There is only one row. The printed table is [[9, 0, 0], [9, 0, 0]].
g = [[0] * 3] * 2g[0][0] = 9print(g)[[9, 0, 0], [9, 0, 0]]
int[] row = {0, 0, 0};int[][] g = {row, row};g[0][0] = 9;System.out.println(Arrays.deepToString(g));[[9, 0, 0], [9, 0, 0]]
Building a table: the fix
Make a new row on every pass of a loop. Each pass builds its own list of zeros and adds it to the table, so the rows are separate, and a change to one cell stays in its row.
g = []for i in range(2): g.append([0, 0, 0])g[0][0] = 9print(g)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
Python runs in the browser below; Java is traced above.
Change the 2 to 3 and run again for a table of three rows. Then change the first line to g = [[0, 0, 0]] * 2 and remove the loop, and see the trap for yourself.
Check yourself
A teacher wants to look up a student by name and get all of that student's marks. Which shape fits?
A hall has three rows of four seats, and the program needs to say whether seat 2 in row 1 is taken. Which shape fits?
g is built from one row written twice. What does this print?
row = [0, 0]g = [row, row]g[1][0] = 7print(g)int[] row = {0, 0};int[][] g = {row, row};g[1][0] = 7;System.out.println(Arrays.deepToString(g));Honesty notes
- Use a flat list when there is one row of values, a list of rows when values sit in rows and columns, and a dictionary of lists when groups have names.
- Build a table with a loop that makes a new row each time, so that the rows are really separate.
- Know the trap: a shortcut that repeats one row gives the table two names for the same row, and a change to one cell shows up in both rows.
- In Java, a table that grows is a list of lists with the types written out; a table whose size is fixed is simpler as int[][].