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

Free lesson

Lists keep items in order

After this lesson: I can store and change items in a list.

45 min lessonNo commands

Forty parcels in one van

A delivery driver has forty parcels today. They must go out in the right order. The app on the driver's phone keeps them in a list.

Almost every program at work holds a list of something: orders, tickets, payslips, servers. This lesson shows how to store one and change it.

By the end of this lesson

Note

I can store and change items in a list.

Key words

WordWhat it meansExample
Data structurea way to keep data so a program can use it
Listitems kept in order under one name["kettle", "laptop"]
Itemone value in a list
Indexthe position of an item, counted from 0parcels[0]
Appendto add an item to the endorders.append("lamp")
Arraya list stored in one block of memory, side by side

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

A list keeps items in order

A list holds many items under one name. The items stay in the order you put them in.

Each item has an index, its position. The first item is at index 0, the second at index 1.

You ask for an item with square brackets: parcels[0]. len(parcels) tells you how many items there are.

Try it: read items by index

Code: python
# Today's parcels for one delivery van, in delivery order.
parcels = ["kettle", "laptop", "shoes", "printer ink"]

print(parcels[0])     # the first parcel
print(parcels[2])     # the third parcel
print(len(parcels))   # how many parcels

# Change parcels[2] to parcels[3], then press Run.

Worked example: the parcels list

IndexItemHow you ask for it
0kettleparcels[0]
1laptopparcels[1]
2shoesparcels[2]
3printer inkparcels[3]

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

A common mistake

Warning

Many people think the first item is at index 1. In Python, and most languages, it is at index 0. So a list of 4 items ends at index 3. Asking for index 4 stops the program with an IndexError.

You can change a list

A list is not fixed. append adds an item to the end. Putting a new value at an index replaces the item there. remove takes out an item by its value.

A for loop walks through every item in order. You use it to pack, total or check each one.

Try it: change the orders

Code: python
orders = ["kettle", "laptop", "shoes"]

orders.append("desk lamp")   # add to the end
orders[1] = "tablet"          # replace the item at index 1
orders.remove("shoes")        # take an item out by its value
print(orders)

# Walk through every order with a for loop.
for order in orders:
    print("Pack:", order)

# Add a line that appends "phone case", then press Run.

An array keeps items side by side

Many languages, such as Java and C, use an array. Its items sit side by side in memory.

So the computer finds any index in one step. Adding at the end is quick too.

Adding at the front is slower. Every item has to move along one place first. A Python list works the same way inside.

Worked example: adding at the front

Items in the listAdd at the endAdd at the front
51 step5 moves, then 1 step
1001 step100 moves, then 1 step
10,0001 step10,000 moves, then 1 step

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

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.