Many values in one name
After this: Make a list, read a value by its position, and add one at the end.
On this page · 6 sections · 3 checks
A teacher has three marks for one student: 72, 85 and 60. With what you know so far, you would keep them in three variables.
mark1 = 72mark2 = 85mark3 = 60int mark1 = 72;int mark2 = 85;int mark3 = 60;This works for three marks. Now the student sits a fourth test. You need a fourth box, with a fourth name, and every line that works with the marks has to change. And a loop cannot go through the three boxes, because each one is separate.
Many values, one name
The fix is to keep all the marks in one box, in order. A is a row of values under one name. You can add a value, read any value by its place in the row, and go through the whole row with a loop.
Here is the smallest list that does something you can see.
marks = [72, 85, 60]print(marks)[72, 85, 60]
ArrayList<Integer> marks = new ArrayList<>(List.of(72, 85, 60));System.out.println(marks);[72, 85, 60]
The values sit between square brackets, with commas between them. The box marks holds the whole row. Printing the name prints the whole row, brackets and all.
Reading one value by its position
Each value in a list has a position number. To read one value, write the name of the list and then the position, in square brackets: marks[1]in round brackets after get: marks.get(1).
The first position is 0, not 1. That surprises almost everyone.
| Position | 0 | 1 | 2 |
|---|---|---|---|
| Value | 72 | 85 | 60 |
So marks[1]marks.get(1) is 85, the second value, and marks[0]marks.get(0) is 72, the first. The position number is also called the ; this tutorial uses both words.
Work through marks[0] + marks[2]marks.get(0) + marks.get(2) by hand:
- Look at position 0 of the row: 72.
- Look at position 2: 60.
- Add them: 132.
Step through the same three reads in the trace. The box marks holds the whole row; each print picks one value out of it.
marks = [72, 85, 60]print(marks[0])print(marks[2])print(marks[0] + marks[2])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 list as a row of cells
The picture below draws the list as a row of numbered cells, so you can see the positions. Tap through the task: it reads two positions, adds a value at the end, and reads the new last cell. The picture also says a few words about where a cell lives; you can ignore those for now.
- Read index 1Read position 1. The picture calls a position an index; the two words mean the same thing. Position 1 is the second cell.
- Read index 0
- Append 91
- Read index 3
- 072
- 185
- 260
- 3·
Run an operation to begin.
Adding a value at the end
A list can grow. To add a value at the end, write the name of the list, a dot, and append(91)add(91). This is a line that works on the list: the list named before the dot is the one that changes.
To ask how many values a list holds, use its : len(marks)marks.size(). After adding 91 to the three marks, the length is 4, and the last position is 3, one less than the length.
Run the program below and step through it. Watch the row grow on line 38, and watch which value line 49 picks out.
- Run the programRun it and step through. The box marks holds the whole row. Watch the row grow on line 3 (Java: line 8), and watch which value line 4 (Java: line 9) picks out.
marks = [72, 85, 60]print(marks)marks.append(91)print(marks[1])print(len(marks))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 when you press Run on the block below. Java does not run here: if you are learning Java, step through the trace above instead, and paste the code into a Java tool on your computer when you want to run it for real.
Change the 1 in the fourth line to 0, or to 3, and run again. Then add a fifth mark and see the length change.
Check yourself
The list marks holds [72, 85, 60]. What is marks[1]marks.get(1)?
What does this print?
marks = [72, 85, 60]marks.append(91)print(marks[3])print(len(marks))ArrayList<Integer> marks = new ArrayList<>(List.of(72, 85, 60));marks.add(91);System.out.println(marks.get(3));System.out.println(marks.size());Complete the missing line so that 91 is added at the end and the program prints [72, 85, 60, 91].
marks = [72, 85, 60]____________________ # ← add 91 at the endprint(marks)ArrayList<Integer> marks = new ArrayList<>(List.of(72, 85, 60));____________________ // ← add 91 at the endSystem.out.println(marks);Honesty notes
- Make a list: many values in order, under one name, written between square brackets in Python and built with new ArrayList in Java.
- Read one value by its position (also called its index), and remember that the first position is 0.
- Add a value at the end (append in Python, add in Java) and ask for the length (len in Python, size in Java).
- Say that the last position is one less than the length.