Drop links or images here to add them to the editor.

In this topic students meet their first data structure: the list, which stores many pieces of data under one identifier.

Learning objectives and goals

Be able to read, comprehend, trace, adapt and create Python code that:

Slide deck

The slide deck for this topic is on Google Slides. A PDF copy is kept on Dodona too.

Teacher notes

A list is an example of a data structure. That is the name for a number of different programming techniques used to organise and manipulate data in programs.

So far, to store data in a program, we have used variables, which hold one piece of data and use an identifier (the variable name) to represent it in the code.

Lists work in the same way as variables, but they store multiple items of data using one identifier.

Lists are created in Python like this:

countries = ["UK", "USA", "Chad", "Australia", "Thailand"]

prime_numbers = [1, 3, 5, 7, 11, 13]

Notice that the list containing string (text) data has each item in speech marks. The list containing integer (whole number) data does not need them.

To identify individual items in a list, the data is indexed, meaning each item is given a number. Indexing starts at 0, so the first item in a list will be number 0, the second number 1, and so on.

Lists: key concepts

You may hear lists referred to as “arrays” in other programming languages. There are small differences, but at this stage we will treat them as essentially the same thing.

Output from lists

The print command is used to output data from lists, just as it is to output from a variable.

Output one item from a list

To output one item, use print followed by square brackets with the item’s index number:

prime_numbers = [1, 3, 5, 7, 11, 13]

print(prime_numbers[0])

This would print 1 from the prime_numbers list, as that is the data at index 0.

countries = ["UK", "USA", "Chad", "Australia", "Thailand"]

print(countries[2])

This would print Chad from the countries list, as that is the data at index 2.

Output every item in a list

Python makes it simple to output a whole list:

print(prime_numbers)

Worth showing students what that actually produces, because it is not the same as printing the items one at a time:

[1, 3, 5, 7, 11, 13]

The square brackets are part of the output, the items are separated by a comma and a space, and in a list of text every item comes out inside quotes. Students who expect one item per line are surprised by this, and the exercises in this series check the exact form.

Let the user choose which item to output

To do this, we get the user to input a number and store it in a variable. In the print statement, replace the number in square brackets with this variable:

num1 = int(input())

print(countries[num1])

The cast to int is not optional here: an index has to be a whole number, and input always returns text.

Assigning to lists

Data in lists can be edited in the same way that data in a variable can. Let us use the countries list from earlier in this document as an example:

countries = ["UK", "USA", "Chad", "Australia", "Thailand"]

The = symbol is used to assign new data. This code replaces the Australia in the countries list with Mexico:

countries[3] = "Mexico"

This code concatenates (joins) ingdom to the end of UK. countries[0] will now contain UKingdom. Not a great example, I know, but it works:

countries[0] = countries[0] + "ingdom"

Now let us use the prime_numbers list to see some maths examples. This list contains integers, so we can do maths with them:

prime_numbers = [1, 3, 5, 7, 11, 13]

This code adds one to the first item in the list. prime_numbers[0] will now contain 2:

prime_numbers[0] += 1

This code multiplies the second and third items in the list and stores the result in the total variable. total will now contain 15:

total = prime_numbers[1] * prime_numbers[2]

This code subtracts the fourth item from the fifth and stores the result in the first position. prime_numbers[0] will now contain 4:

prime_numbers[0] = prime_numbers[4] - prime_numbers[3]

Adding to and removing from lists

These activities look at how we add and remove items from a list. There are two different ways of doing each of these tasks, and they work in slightly different ways.

Adding items

list_name.append("item")

adds an item to the end of the list.

list_name.insert(position_number, "item")

inserts the item at the position given in the brackets. The number comes first, then a comma, then the item.

Removing items

list_name.remove("item")

searches through the list and removes the item in brackets.

list_name.pop()

removes the last item in the list.

list_name.pop(item_index)

removes the item at the index in the brackets.

Checking whether an item is in the list

Python makes it really easy to check if an item is in a list. This is where we can start to combine what we have previously learned about selection with lists. The code for this is:

if "item" in list_name:
    print("Found it")

Errors and misconceptions to watch out for

Make sure that students check for the following things: