Home / Reasoning / Lists: Store Many Values and Loop Through Each One
Reasoning & Coding · Coding
Lists: Store Many Values and Loop Through Each One
Keep many values in one list, read an item by its position, then use For each to say, add up or count every item and fix common list bugs.
The idea
Imagine keeping the scores of a whole class in thirty separate variables: score1, score2, score3 and so on. Adding them up would take thirty lines. A list solves this. A list is one name that holds many values in order, like a numbered shopping list on the fridge.
In this lesson, Set points to [4, 7, 2] makes a list called points with three items. Each item has a position. item 1 of points is 4, item 2 of points is 7 and item 3 of points is 2. length of points tells you how many items there are: 3. Like Scratch, these lists count from item 1. (Some languages, such as Python, count from 0, so always check which way a language counts.)
A list can grow. Add 5 to points puts 5 at the end, so the list becomes [4, 7, 2, 5] and its length becomes 4.
The real power comes from looping through a list. For each p in points [say p] runs the steps in the brackets once for every item. The first time, p is 4; the next time, p is 7; then p is 2. You do not need to know how long the list is: the loop stops after the last item.
Lists and variables work well together. To add up a list, set a total to 0 before the loop and change it by each item inside the loop. To count items that pass a test, put an If inside the loop. A trace table with one row per item keeps you from losing your place.
As in earlier lessons, F moves the robot forward one square, L and R turn it a quarter turn without moving, S is the start, G is the goal and dark squares are walls. Unless a question says otherwise, the robot starts on S facing right (east).
Worked examples
Example 1 · Read a list by position
Set snacks to [apple, pear, plum]. What is item 2 of snacks, and what is length of snacks?
- Count the items from the left, starting at 1: apple is item 1, pear is item 2, plum is item 3.
- item 2 of snacks is pear.
- There are three items, so length of snacks is 3.
item 2 is pear, and the length is 3.
Example 2 · Add up a list
Set points to [4, 7, 2]. Set total to 0. For each p in points [Change total by p]. say total. What is said?
- Make a trace table with columns p and total. total starts at 0.
- p = 4: total becomes 0 + 4 = 4.
- p = 7: total becomes 4 + 7 = 11.
- p = 2: total becomes 11 + 2 = 13. The loop ends after the last item, then say total runs once.
It says 13.
Example 3 · Count the items that pass a test
Set scores to [6, 3, 9, 5, 8]. Set count to 0. For each s in scores [If s > 5 [Change count by 1]]. say count. What is said?
- Check each item against the test s > 5.
- 6 yes (count 1), 3 no, 9 yes (count 2), 5 no, because 5 is not greater than 5, and 8 yes (count 3).
- After the loop, count is 3.
It says 3.
Example 4 · A list of moves for the robot
Set moves to [3, 2]. For each n in moves [Repeat n [F] R]. Where does the robot finish?
- First item, n = 3: the robot moves 3 squares right to the top-right corner, then turns right to face down.
- Second item, n = 2: it moves 2 squares down and lands on G, then turns right again to face left.
- The list holds the trip; changing the list changes the trip without changing the loop.
It finishes on G, facing left.
Example 5 · Build a list with a loop
Set squares to []. Set n to 1. Repeat 4 [Add n × n to squares; Change n by 1]. What is the list at the end?
- [] is an empty list with length 0.
- n = 1: add 1. n = 2: add 4. n = 3: add 9. n = 4: add 16.
- Each Add puts the new item at the end, so the order stays the same as the order of the loop.
squares is [1, 4, 9, 16].
Common mistakes
Why this fails: In this lesson (and in Scratch), the first item is item 1. If you count from 0, every answer is one item off.
Why this fails: In [5, 10, 15], item 2 is the value 10. The 2 is where it sits; the 10 is what is stored there.
Why this fails: Then the total is wiped out on every pass and only the last item is left. Set the starting value once, before the loop.
Why this fails: At least 5 includes 5, so the test is s ≥ 5. The test s > 5 leaves out every 5 in the list.
Why this fails: For each runs once per item and stops after the last one, so the same loop works for a list of 3 items or 300.
Foundation
Foundation practice
Read items by position and trace a simple For each loop.
1. What is a list in a program?
- One name that holds many values in order
- A command that moves the robot forward
- A variable that can only hold one number
- A loop that repeats exactly 10 times
2. Set colours to [red, blue, green, yellow]. What is item 3 of colours?
- blue
- yellow
- green
- 3
3. Set jumps to [5, 10, 15, 20, 25]. What is length of jumps?
- 25
- 75
- 4
- 5
4. Set pets to [cat, dog]. For each p in pets [say p]. What is said?
- cat, then dog
- pets
- p, then p
- dog, then cat
5. Set nums to [2, 4, 6]. For each n in nums [say n × 10]. What is said?
- 2, then 4, then 6
- 20, then 40, then 60
- 120
- 60
Practice
Practice practice
Use a variable with the loop to add up, count and find the biggest.
6. Set points to [5, 3, 8, 4]. Set total to 0. For each p in points [Change total by p]. say total. What is said?
- 16
- 20
- 8
- 4
7. Set scores to [7, 2, 5, 9, 6]. Set count to 0. For each s in scores [If s > 5 [Change count by 1]]. say count. What is said?
- 4
- 5
- 2
- 3
8. Set pets to [cat, dog]. Add fish to pets. What is item 3 of pets now?
- fish
- cat
- dog
- There is no item 3
9. For each n in moves [Repeat n [F] R]. Which list of moves takes the robot from S to G?
- [2, 2, 2]
- [3, 1, 2]
- [3, 2, 2]
- [3, 2, 1]
10. Set L to [8, 3, 5, 1]. Ben wants the last item and writes item 3 of L. What does he get, and what should he write?
- He gets 1; item 3 is right
- He gets 5; he should write item 4 of L
- He gets 3; he should write item 1 of L
- He gets 5; he should write item 5 of L
11. Set best to 0. For each s in [4, 9, 2, 7] [If s > best [Set best to s]]. say best. What is said?
- 7
- 4
- 22
- 9
12. Set prices to [3, 5, 2]. Set total to 0. For each p in prices [Change total by p × 2]. say total. What is said?
- 20
- 10
- 16
- 30
Think
Think practice
Find the bug and explain why lists help.
13. Program: Set points to [4, 7, 2]. For each p in points [Set total to 0; Change total by p]. say total. It says 2 instead of 13. What is the bug?
- The list should be in a different order.
- Set total to 0 is inside the loop, so the total restarts on every pass; move it before the loop.
- Change total by p should be Change p by total.
- say total should be inside the loop.
14. Program A says each item of [2, 5, 1]. Program B says each item of [1, 5, 2]. Which is true?
- They say exactly the same thing.
- B says more numbers than A.
- They say the same numbers in a different order, and the totals of the two lists are both 8.
- A crashes because the list is not in order.
15. Set nums to [5, 8, 3, 5]. You want to count the items that are at least 5. Which test belongs in the If?
- n > 5
- n < 5
- n = 8
- n ≥ 5
16. A teacher has 30 test scores. Give two reasons why one list is better than 30 separate variables.
17. Write a program that says the total of the list [6, 1, 4] using For each.
Challenge
Challenge practice
Build lists with loops and use two lists together.
18. Set squares to []. Set n to 1. Repeat 4 [Add n × n to squares; Change n by 1]. What is item 3 of squares?
- 3
- 9
- 16
- 6
19. Set a to [1, 2, 3]. Set b to [10, 20, 30]. Set i to 1. Repeat 3 [say item i of a + item i of b; Change i by 1]. What is said?
- 11, then 22, then 33
- 66
- 1, then 2, then 3
- 11, then 21, then 31
20. For each n in [2, 2, 2, 2] [Repeat n [F] R]. How many squares does the robot move, and where does it finish?
- 4 moves, 2 squares right of the start
- 8 moves, 8 squares right of the start
- 8 moves, back at the start facing right
- 2 moves, back at the start facing right
21. Write a list of moves so that For each n in moves [Repeat n [F] R] makes the robot go 1 square right, then 3 squares down. Then add one item so it also goes 2 squares left.
Answer key
- 1. One name that holds many values in order
A list keeps many values under one name, in order. F moves the robot, a plain variable holds one value at a time, and a list is not a loop, although loops often go through lists. - 2. green
Count from 1: red is item 1, blue is item 2, green is item 3. blue comes from counting from 0, yellow is the last item, and 3 is the position, not the value stored there. - 3. 5
There are five items, so the length is 5. 25 is the last value, 75 is the total of the values, and 4 comes from counting the gaps or starting at 0. - 4. cat, then dog
The loop takes the items in order and p becomes each one: cat, then dog. It does not say the list's name or the letter p, and it does not go backwards. - 5. 20, then 40, then 60
Each item is multiplied by 10: 20, 40, 60. 2, 4, 6 forgets the × 10, 120 adds the answers into one total, and 60 only shows the last pass. - 6. 20
5 + 3 + 8 + 4 = 20. 16 stops before the last item, 8 is the biggest item, and 4 is the length (or the last item), not the total. - 7. 3
Only 7, 9 and 6 are greater than 5, so count is 3. 4 wrongly counts the 5, 5 is the length of the list, and 2 misses one of the passing items. - 8. fish
Add puts the new item at the end, so the list is [cat, dog, fish] and item 3 is fish. Add does not put it first, and the list now has three items, so item 3 exists. - 9. [3, 2, 2]
3 right, turn to face down, 2 down, turn to face left, 2 left lands on G. [2, 2, 2] turns down too early and hits the wall, [3, 1, 2] turns left too early and hits the wall, and [3, 2, 1] stops one square to the right of G. - 10. He gets 5; he should write item 4 of L
There are 4 items, so the last one is item 4, which is 1. item 3 is 5. Writing item (length of L) of L would also work for a list of any size. item 5 does not exist, and item 1 is the first item, not the last. - 11. 9
best becomes 4, then 9; 2 and 7 are not bigger than 9, so best stays 9. 7 is just the last item, 4 is the first item, and 22 adds them all up. - 12. 20
Each price is doubled before adding: 6 + 10 + 4 = 20. 10 forgets to double, 16 adds 2 instead of multiplying by 2, and 30 multiplies the prices together. - 13. Set total to 0 is inside the loop, so the total restarts on every pass; move it before the loop.
Each pass wipes the total back to 0, so only the last item, 2, survives. Reordering the list would only change which number survives, swapping total and p changes the wrong variable, and moving say inside would say 4, 7, 2 rather than 13. - 14. They say the same numbers in a different order, and the totals of the two lists are both 8.
A list keeps its order, so A says 2, 5, 1 and B says 1, 5, 2. Both have three items that add up to 8. A list does not need to be sorted to work. - 15. n ≥ 5
At least 5 includes 5, so n ≥ 5 counts 5, 8 and 5: 3 items. n > 5 counts only the 8, n < 5 counts the 3, and n = 8 only finds one exact value. - 16. Reasons include: one For each loop can add up or check every score in a few lines; the same program works if a new student joins and the list grows; and you can find any score by its position.
Any two clear reasons are fine. Strong answers say that one loop can work through every item, however long the list is. - 17. Set nums to [6, 1, 4]. Set total to 0. For each n in nums [Change total by n]. say total. It says 11.
Set total to 0 goes before the loop, Change total by n goes inside it, and say total goes after it, so it runs once at the end. - 18. 9
The list becomes [1, 4, 9, 16], so item 3 is 9. 3 is the position, 16 is item 4, and 6 adds 3 + 3 instead of multiplying. - 19. 11, then 22, then 33
i goes 1, 2, 3, so the program adds matching items: 1 + 10, 2 + 20, 3 + 30. 66 is the grand total, 1, 2, 3 says only list a, and 11, 21, 31 adds the same number from a every time instead of the matching item. - 20. 8 moves, back at the start facing right
Four sides of 2 moves is 8 moves, and four right turns make a full turn, so the robot is back where it began, facing right. 4 moves counts the items, 8 squares right ignores the turns, and 2 moves uses only one item. - 21. moves = [1, 3]; then Add 2 to moves, giving [1, 3, 2].
Each item is one straight stretch, and the R after it turns the robot for the next stretch: right, then down, then left. Adding to the end of the list adds a stretch to the end of the trip.