Key Stage 3 computing · Programming
KS3 lists, tables and arrays: a guide for parents
A data structure is a way of storing many related values together under one name, so that a program can work with a whole collection of scores, names or readings instead of a single value. In Years 7 to 9, children learn to use structures such as lists, tables or arrays in their programs and to choose the right one for the job.
Lists, tables and arrays, explained
Imagine a program that records the scores of 30 pupils in a class quiz. Writing score1, score2, score3 and so on up to score30 would be tiring and would break as soon as the class size changed. A list (in some languages called an array) solves this: one name, such as scores, holds an ordered collection of values. The program can then add a score, remove one, count them, search for one or work through all of them with a loop.
Each item in a list has a position number, called its index, and in most languages, including those schools commonly teach, the first item has index 0, not 1. In a list of four items the indexes are 0, 1, 2 and 3. This catches nearly every beginner out. Asking for an index that does not exist, such as index 4 in that list, usually causes an error. Loops that go one step too far or stop one step short are the same off-by-one idea in another form.
The words “list” and “array” are used slightly differently depending on the language. In Python, which many schools use, the structure is called a list and can grow and shrink as the program runs. In some other languages an array has a fixed size and holds items of one data type. The programme of study gives lists, tables and arrays only as examples, so schools choose what to teach, and your child’s teacher will use the word that matches their language.
A table, or two-dimensional structure, has rows and columns, like a spreadsheet or a seating plan. It can be built as a list of lists, and an item is found by giving two positions: the row and then the column. Tables are used to store things like a game board, a set of pupils with several scores each, or pixels in a picture. Children also learn to think about which structure suits a problem, which links to the curriculum’s aim that pupils understand abstraction and data representation.
Most of the work is practical. Typical tasks include a program that stores names and sorts them, a quiz that keeps its questions and answers in two lists, a high-score table, or a noughts and crosses board. Children practise writing loops that visit each item, and algorithms such as finding the largest value or searching for a particular one. Being able to describe in plain words what a loop over a list is doing is as valuable as being able to type it.
Words your child will hear
- List
- An ordered collection of values stored under a single name, such as scores or names.
- Array
- A similar collection, in many languages of fixed size and holding one data type.
- Index
- The position number of an item. In most languages the first item is index 0.
- Element
- One item stored in a list or array.
- Table (two-dimensional list)
- A grid of values with rows and columns, found using two index numbers.
- Traverse
- To visit every item in a list in turn, usually with a loop.
- Length
- How many items a list currently holds.
What the National Curriculum says
The programme of study for Key Stage 3 says:
- Pupils should be taught to use 2 or more programming languages, at least one of which is textual, to solve a variety of computational problems; make appropriate use of data structures [for example, lists, tables or arrays]; design and develop modular programs that use procedures or functions
- Aim: pupils can understand and apply the fundamental principles and concepts of computer science, including abstraction, logic, algorithms and data representation.
Objectives from the National Curriculum in England, © Crown copyright, used under the Open Government Licence v3.0.
A worked example
A program stores four quiz scores in a list: scores = [12, 7, 15, 9]. Find the total, the average and the highest score.
- Count positions from 0. scores[0] is 12, scores[1] is 7, scores[2] is 15 and scores[3] is 9. The length is 4, but scores[4] does not exist and would cause an error.
index: 0 1 2 3 value: 12 7 15 9
- Find the total by starting at 0 and adding each item in turn with a loop. The variable total changes like this: 0, 12, 19, 34, 43.
total = 0 for s in scores: total = total + s - The average is the total divided by the number of items. The length is 4, so the average is 43 ÷ 4 = 10.75.
average = total / len(scores)
- Find the highest by assuming the first item is the highest so far, then checking every item. If one is bigger, it replaces the highest. The value of highest goes 12, 12 (7 is smaller), 15 (bigger), 15 (9 is smaller).
highest = scores[0] for s in scores: if s > highest: highest = s - Check by hand: 12 + 7 = 19, 19 + 15 = 34 and 34 + 9 = 43. The largest number in the list is 15. The code and the hand check agree.
The total is 43, the average is 10.75 and the highest score is 15.
Common mistakes and how to help
Thinking the first item is at position 1
In everyday life we count from one: first, second, third. So scores[1] feels as if it should be the first score, and scores[0] looks like nothing at all.
What helps: Describe the index as “how many steps from the start”. The first item is zero steps away. Floors in a building work similarly: the ground floor is 0 and the first floor is one step up.
Mixing up the index and the value stored there
Both are numbers. In scores = [12, 7, 15, 9], scores[2] is 15, not 2 and not 7, and children confuse the position with what is in it.
What helps: Draw the list as boxes with the index written underneath each box and the value inside it. Ask “which box?” before asking “what is in it?”.
Using lots of separate variables instead of one list
Variables are learned first, so score1, score2 and score3 feel natural. It works with three scores, then falls apart.
What helps: Ask what would happen with 100 scores, or if the number of scores changed each time the program ran. A list with a loop handles any size.
Getting rows and columns the wrong way round in a table
In a table such as grid[1][2], the first number picks the row and the second the column, but children often expect (across, down) like coordinates on a graph.
What helps: Remember “row first, then column”, the way you read a table. Practise by writing the contents of a small grid and asking for particular cells, remembering that both counts start at 0.
How to help at home
- Number a list from zero. Write a short shopping list and number the items starting from 0. Ask “what is item 2?” and “which item is at the last position?”. It feels odd for a few minutes and then becomes automatic.
- Use an egg box or a board as a table. An egg box, a chess board or a noughts and crosses grid is a table with rows and columns. Ask your child to name a square using its row and column numbers, starting each from 0, and to say how many squares there are altogether.
- Find the biggest by scanning. Lay out five or six cards face up with different numbers. Ask your child to find the largest while looking at only one card at a time and remembering only a single “best so far”. This is exactly what the loop in a program does.
- Spot lists and tables in daily life. Ask which things are lists (a playlist, a queue, a row of coat pegs) and which are tables (a timetable, a league table, a calendar). Then ask what you would need to know to find one item.
- Ask which structure and why. When your child describes a project, ask what they stored the data in and why. If they used a list, ask what the index of the last item is. If they did not, ask what would happen with 50 items.
Related topics
Builds on
- Key Stage 2: Designing, writing and debugging programs
- Key Stage 2: Controlling and simulating physical systems
- Key Stage 2: Decomposition: breaking problems down
- Key Stage 2: Sequence in programs
- Key Stage 2: Selection: if and else
- Key Stage 2: Repetition and loops
- Key Stage 2: Variables, input and output
- Key Stage 2: Explaining how algorithms work
- Key Stage 2: Debugging algorithms and programs
More on programming
- Key Stage 3: Functions and procedures
- Key Stage 3: Text-based programming
Updated .