Specialty Digest

Global English editionLearners’ Study Dashboard — in development
The Knowledge Library for All

Online Courses / University Track / Computing Foundations

Module 6 · Lesson 3 · Core

Storing data: lists and dictionaries

Choose a list, a dictionary or a table of records for data, and read items from each.

  • 50 minutes
  • Serves O6
  • Assumes M6.L2

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.

4081152163Positions 0, 1, 2, 3; the item at position 2 is 15
A list stores items in order. Many programming languages count positions from 0. Invented list.

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)
Ana101
Raj102
Lin103

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.

StructureFind an item byGood for
ListPosition (0, 1, 2, …)Ordered items, such as scores in a test
DictionaryKey (such as a name)Looking up a value by label
Table of recordsRow and field nameSeveral 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.

  1. Positions start at 0: 4 is at 0, 8 at 1, 15 at 2, 16 at 3. (1 mark)
  2. Position 1 holds 8. (1 mark)
  3. Position 3 holds 16. (1 mark)
  4. 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:

2. A dictionary finds a value by:

3. To look up a phone extension by name, use:

4. The length of the list 4, 8, 15, 16 is:

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

MistakeWhy it fails
Counting positions from 1In many languages the first position is 0.
Using a list for lookup by nameUse a dictionary.
Confusing length with last positionThe last position is one less than the length.

Limits. Some languages count positions from 1; check the rule of the language you use.