Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

Lists and arrays

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

45 minutes

One name, many things

You make a shopping list. It has milk, eggs and bread. You do not make three separate notes.

A program needs the same trick. It needs one name for many values: the scores of 30 students, or the songs in a playlist. A list does this job.

Key words

WordWhat it meansExample
Data typeThe kind of value, such as a whole number or text.integer, string
ListOne name that holds many values in order.["milk", "eggs", "bread"]
ArrayA list of values that are all the same data type.[10, 20, 30]
ElementOne value inside a list."eggs"
IndexThe position number of an element. It starts at 0.index 0 is the first
AppendAdd an element to the end.items.append("tea")
Data structureA way to organise many values.list, dictionary
DictionaryA data structure of keys and values.{"Sam": 16}

The cells students fill in are left blank in this preview.

Try it: make a list

Code: python
shopping = ["milk", "eggs", "bread"]
print(shopping)
print(len(shopping))
print(shopping[0])
print(shopping[2])

# len() counts the elements.
# Change shopping[2] to shopping[1] and run again.

Worked example: the index starts at 0

Index012
Element"milk""eggs""bread"
Count (what a person says)firstsecondthird

The cells students fill in are left blank in this preview.

Counting starts at 0

data-callouts.warning.label

The first element is at index 0, not 1. A list with 3 elements has indexes 0, 1 and 2. Asking for index 3 gives an IndexError, because there is no fourth element.

Which index?

Have a go

Which index?

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

List or array?

A list can hold mixed types, such as a name, an age and a score. An array holds values that are all the same type, such as ten scores. In many languages an array also has a fixed size.

Python has no built-in array. We use a list as an array when all the values have the same type. Both use an index.

The list commands you need

CodeWhat it doesResult (each row starts with items = ["a", "b", "c"])
items.append("d")Add to the end["a", "b", "c", "d"]
items.insert(1, "x")Put "x" at index 1, push others along["a", "x", "b", "c"]
items.remove("b")Take out the first "b"["a", "c"]
items.pop()Take out the last element and give it backgives "c"; list is ["a", "b"]
items.index("c")Find the index of "c"2
items.count("a")Count how many times "a" is there1
"b" in itemsIs it in the list?True
"z" not in itemsIs it missing?True
items.sort()Put the elements in order["a", "b", "c"]

The cells students fill in are left blank in this preview.

Try it: the list commands

Code: python
items = ["milk", "eggs"]
items.append("bread")
items.insert(0, "tea")
print(items)

items.remove("eggs")
print(items)

last = items.pop()
print(last)
print(items)
print("milk" in items)

# Predict each line before you run it.

The lesson goes on

7 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.