Loops over lists
After this: Write the total, count, biggest and search loops over a list.
On this page · 7 sections · 3 checks
A teacher has the marks of every student in one list and wants four things: the total, how many students passed, the best mark, and whether anyone got exactly 60. Four questions, and the same shape of program answers each of them. In "Repeating" you wrote a loop that runs once for each value in a list. This lesson puts that loop to work.
The three-part pattern
Every one of the four programs has three parts.
- Before the loop, make a box for the answer, holding a starting value.
- Inside the loop, change the box using the value the loop is looking at.
- After the loop, print the box.
The loop itself is the one from "Repeating": for n in marks:for (int n : marks) { gives n each value of the list in turn, first to last. Only part 1 and part 2 change from program to program.
Adding up
To add up the marks, the box is total, it starts at 0, and each pass adds the current value to it. Work through [72, 85, 60, 91] by hand:
totalstarts at 0.- The loop looks at 72:
totalbecomes 72. - It looks at 85:
totalbecomes 157. - It looks at 60:
totalbecomes 217. - It looks at 91:
totalbecomes 308. - The loop is finished. The print shows 308.
A box that is updated on every pass like this is called a . Watch it in the trace: the box total should change four times before the print.
- Run the programRun it and watch the box total. It should change four times, once for each value in the list, before the print.
marks = [72, 85, 60, 91]total = 0for n in marks: total = total + nprint(total)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
Add a fifth mark to the list and run again; the total should rise by that mark.
Counting
To count how many marks are at least 70, the box is count, it starts at 0, and it goes up by one only when the value passes the test. The loop looks at every value, but the box changes only on a yes.
marks = [72, 85, 60, 91]count = 0for n in marks: if n >= 70: count = count + 1print(count)3
ArrayList<Integer> marks = new ArrayList<>(List.of(72, 85, 60, 91));int count = 0;for (int n : marks) { if (n >= 70) { count = count + 1; }}System.out.println(count);3
72, 85 and 91 answer yes to n >= 70, so count ends at 3. 60 answers no and changes nothing.
Finding the largest
To find the biggest mark, the box is biggest, and it starts as the first value in the list, not as 0. Each pass asks: is this value bigger than the box? If yes, the box takes the value. If no, nothing happens.
biggeststarts as 72, the first value.- 72 is not bigger than 72. No change.
- 85 is bigger than 72:
biggestbecomes 85. - 60 is not bigger than 85. No change.
- 91 is bigger than 85:
biggestbecomes 91. - The print shows 91.
marks = [72, 85, 60, 91]biggest = marks[0]for n in marks: if n > biggest: biggest = nprint(biggest)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
Why start with the first value and not with 0? Because 0 is not in the list. If every mark were below 0, a box starting at 0 would never change, and the program would print a value that nobody got. The challenge at the end of this tutorial has a program with exactly that mistake.
Searching
To find out whether 60 is in the list, the box is found, it starts as Falsefalse, and it becomes Truetrue when the loop meets 60. Once it is Truetrue, nothing turns it back.
marks = [72, 85, 60, 91]found = Falsefor n in marks: if n == 60: found = Trueprint(found)True
ArrayList<Integer> marks = new ArrayList<>(List.of(72, 85, 60, 91));boolean found = false;for (int n : marks) { if (n == 60) { found = true; }}System.out.println(found);true
Four programs, one shape. When you meet a new question about a list, ask yourself: what is the box, what does it start as, and what does one pass do to it?
Check yourself
What does this print?
prices = [10, 20, 5]total = 0for p in prices: total = total + pprint(total)ArrayList<Integer> prices = new ArrayList<>(List.of(10, 20, 5));int total = 0;for (int p : prices) { total = total + p;}System.out.println(total);What does this print?
scores = [4, 9, 2]biggest = scores[0]for s in scores: if s > biggest: biggest = sprint(biggest)ArrayList<Integer> scores = new ArrayList<>(List.of(4, 9, 2));int biggest = scores.get(0);for (int s : scores) { if (s > biggest) { biggest = s; }}System.out.println(biggest);Complete the missing line so that the program counts the marks that are at least 70 and prints 3. Write the whole line, including the colon at the endbrackets and the opening brace.
marks = [72, 85, 60, 91]count = 0for n in marks: ____________________ # ← is this mark at least 70? count = count + 1print(count)ArrayList<Integer> marks = new ArrayList<>(List.of(72, 85, 60, 91));int count = 0;for (int n : marks) { ____________________ // ← is this mark at least 70? count = count + 1; }}System.out.println(count);Honesty notes
- Write the three-part pattern: a box before the loop, one change inside it, a print after it.
- Add up a list with a running total and count the values that pass a test.
- Find the largest value by starting from the first one and keeping the bigger of the two each time.
- Search for a value with a yes/no box that becomes true when the value is seen.