Phase 1: Prepare
Step 1 Warm-up recall
From M6.L2: what does a function give back?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- read an item from a list by position (Foundation);
- look up a value by key (Core);
- choose a structure for given data (Core).
Phase 2: Teach
Step 3 A familiar example
A queue of people is a list: order matters. A phone directory is a dictionary: you find a number by a name, not by a position.
Step 4 Clear explanation
A data structure is a way of organising data so it can be used. A list stores items in order, and each item has a position (its index). Many programming languages count positions from 0.
In the figure, the list 4, 8, 15, 16 has length 4; the item at position 0 is 4, the item at position 2 is 15 and the last item is at position 3. A dictionary stores pairs, each with a key and a value, and you look up a value by its key.
| Key (name) | Value (extension, invented) |
|---|---|
| Ana | 101 |
| Raj | 102 |
| Lin | 103 |
Looking up the key Raj gives 102. A table of records keeps several facts about each thing, one row per thing. To choose a structure, ask how you will find an item.
| Structure | Find an item by | Good for |
|---|---|---|
| List | Position (0, 1, 2, …) | Ordered items, such as scores in a test |
| Dictionary | Key (such as a name) | Looking up a value by label |
| Table of records | Row and field name | Several facts about each thing |
Using a dictionary for lookup by name avoids searching a long list from the start, which links back to the cost of linear search in Module 5. The entry on object-oriented programming shows a more advanced way of grouping data with the functions that use it.
Step 5 Worked example (4 marks)
The list is 4, 8, 15, 16. Give the item at position 1, the item at position 3 and the length.
- Positions start at 0: 4 is at 0, 8 at 1, 15 at 2, 16 at 3. (1 mark)
- Position 1 holds 8. (1 mark)
- Position 3 holds 16. (1 mark)
- Answer: 8, 16 and length 4. (1 mark)
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. In the list 4, 8, 15, 16, the item at position 2 is:
Positions count from 0.
2. A dictionary finds a value by:
Each value is stored under a key.
3. To look up a phone extension by name, use:
The name is the key.
4. The length of the list 4, 8, 15, 16 is:
It holds four items.
Phase 4: Close
Step 7 Recap
- A list is ordered and found by position from 0.
- A dictionary is found by key.
- Choose a structure by how you will look items up.
Step 8 Exit ticket (ungraded)
For the list 7, 2, 9, 5 give the item at positions 0 and 3.
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Read about object-oriented programming and note how it groups data with functions.
Common mistakes
| Mistake | Why it fails |
|---|---|
| Counting positions from 1 | In many languages the first position is 0. |
| Using a list for lookup by name | Use a dictionary. |
| Confusing length with last position | The last position is one less than the length. |
Limits. Some languages count positions from 1; check the rule of the language you use.