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
| Word | What it means | Example |
|---|---|---|
| Data structure | a way to keep data so a program can use it | |
| List | items kept in order under one name | ["kettle", "laptop"] |
| Item | one value in a list | |
| Index | the position of an item, counted from 0 | parcels[0] |
| Append | to add an item to the end | orders.append("lamp") |
| Array | a 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
# 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
| Index | Item | How you ask for it |
|---|---|---|
| 0 | kettle | parcels[0] |
| 1 | laptop | parcels[1] |
| 2 | shoes | parcels[2] |
| 3 | printer ink | parcels[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
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 list | Add at the end | Add at the front |
|---|---|---|
| 5 | 1 step | 5 moves, then 1 step |
| 100 | 1 step | 100 moves, then 1 step |
| 10,000 | 1 step | 10,000 moves, then 1 step |
The cells students fill in are left blank in this preview.