Free lesson with a practice sheet
Lists and arrays
A list or array stores many values under one name; learn about index numbers starting at 0, and loop through a list to add or find things.
Let us begin
The big idea
A class has 40 students. Making 40 separate variables for their marks would be painful. Instead, a program keeps all the marks in one list, also called an array. Each value sits in a numbered slot so the program can reach any of them.
A list is a row of values stored under one name. We write it with square brackets: marks = [72, 85, 90, 64]. This list has four items. Its length is 4.
Each slot has an index number. In most programming languages the first slot is index 0, not 1. So marks[0] is 72, marks[1] is 85, marks[2] is 90 and marks[3] is 64. The last index is always one less than the length. Asking for marks[4] is an error because there is no such slot.
You can change one slot: marks[1] = 88 replaces 85 with 88. You can add to the end: append 70 makes the list [72, 88, 90, 64, 70] with length 5.
The real power comes from loops. To add all the marks: total = 0, then FOR i FROM 0 TO length - 1: total = total + marks[i]. To find the largest: biggest = marks[0], then for each item, IF item > biggest THEN biggest = item. The same short loop works for a list of 4 or 4,000.
Worked examples
See how it works
Reading by index
fruits = ["mango", "apple", "banana", "guava"]. fruits[0] is "mango", fruits[2] is "banana", and fruits[3] is "guava". The length is 4 and the highest valid index is 3.
Adding up a list
marks = [72, 85, 90, 64]. Trace total: start 0; i = 0, total = 72; i = 1, total = 157; i = 2, total = 247; i = 3, total = 311. The sum is 311, and the average is 311 divided by 4 = 77.75.
Finding the largest
nums = [14, 3, 27, 9]. biggest = 14. Compare 3: not bigger. Compare 27: bigger, so biggest = 27. Compare 9: not bigger. Final answer 27.
Counting matches
temps = [31, 35, 29, 36, 33]. Count the days above 32: count = 0; 31 no; 35 yes, count 1; 29 no; 36 yes, count 2; 33 yes, count 3. Three hot days.
Try it at home
An egg-tray array
You will need
- An empty egg tray or a row of six cups
- Small slips of paper or beans
- A pencil and paper
What to do
Write the index numbers 0, 1, 2, 3, 4, 5 on paper slips and place one in front of each cup. The first cup is 0.
Put a different number of beans in each cup, for example 3, 7, 2, 5, 1, 4. This is your list: beans = [3, 7, 2, 5, 1, 4].
Ask: what is beans[2]? Count the beans in the cup labelled 2 (answer 2). What is beans[5]? (answer 4).
Now find the largest by looping: start with cup 0 as biggest and compare each next cup in turn, saying aloud whether you change your answer.
Add up all the cups one at a time, writing the running total after each cup.
Practice sheet
Check what you learned
Practice sheet: Lists and arrays (Classes 9-10, Computers and coding)
- colours = ["red", "green", "blue"]. What is colours[0]?
- What is the length of the list above, and what is its highest valid index?
- nums = [5, 9, 2, 8]. What is nums[3]?
- After nums[1] = 10, what is the list?
- Add up [4, 11, 6, 9]. What is the total?
- What is the largest value in [17, 42, 8, 35]? Show the comparisons.
- Why is asking for marks[4] an error when the list has 4 items?
- How many items does a list with highest index 9 contain?
- In pseudocode, write a loop that counts how many items in a list are equal to 0.
- Why is a list better than 40 separate variables for 40 marks?
Answer key
Answers: Lists and arrays
- "red"
- Length 3, highest valid index 2.
- 8
- [5, 10, 2, 8]
- 30
- 42: start 17; 42 is bigger, so biggest = 42; 8 is not; 35 is not.
- Indexes run from 0 to 3, so there is no slot numbered 4.
- 10 items (indexes 0 to 9).
- count = 0; FOR i FROM 0 TO length - 1: IF list[i] = 0 THEN count = count + 1.
- One short loop can handle every mark, however many there are, and the list can grow or shrink.
