Which orders went to Leeds?
You start a job at an online shop. Your manager sends you last week's orders and asks a simple question.
"How many orders went to Leeds?"
There are 4,000 rows. Nobody reads 4,000 rows by eye. A few lines of Python can answer it in a second.
By the end of this lesson
Note
I can read and filter rows of data in Python.
Key words
| Word | What it means | Example |
|---|---|---|
| Row | one record, such as one order | |
| Column | one kind of value every row has | city |
| Dictionary | Python values looked up by a name | {"city": "Leeds"} |
| List of dictionaries | a table: one dictionary for each row | |
| Filter | to keep only the rows that pass a test |
The cells students fill in are left blank in this preview.
A row is a dictionary
A spreadsheet has columns across the top and one row for each order.
In Python, one row is a dictionary. Each column name is a key, and each key holds that row's value.
The whole table is a list of those dictionaries. orders[0] is the first row. orders[0]["city"] is the city of that row.
Read a row and a value
# Each order is one row: a dictionary of column name to value.
orders = [
{"id": 101, "city": "Leeds", "total": 24.50},
{"id": 102, "city": "Bristol", "total": 8.99},
{"id": 103, "city": "Leeds", "total": 61.00},
]
first = orders[0]
print("Rows in the table:", len(orders))
print("First order id:", first["id"])
print("First order city:", first["city"])
# Change the last line to print the total of the second order.
print("Second order city:", orders[1]["city"])
The same orders as a table
| Python | What it gives | Value |
|---|---|---|
| len(orders) | how many rows | 3 |
| orders[0] | the first row | the whole order 101 |
| orders[0]["city"] | one value in the first row | Leeds |
| orders[2]["total"] | one value in the third row | 61.0 |
The cells students fill in are left blank in this preview.
A loop and an if filter rows
To filter, you look at every row in turn. A for loop does that.
Inside the loop, an if tests the row. When the test is true, you add the row to a new list with append.
When the loop ends, the new list holds only the rows you wanted. len then counts them.
Keep the orders from Leeds
orders = [
{"id": 101, "city": "Leeds", "total": 24.50},
{"id": 102, "city": "Bristol", "total": 8.99},
{"id": 103, "city": "Leeds", "total": 61.00},
{"id": 104, "city": "Cardiff", "total": 15.25},
{"id": 105, "city": "Leeds", "total": 9.75},
]
# Keep only the orders that pass the test, in a new list.
leeds = []
for order in orders:
if order["city"] == "Leeds":
leeds.append(order)
for order in leeds:
print(order["id"], order["total"])
print("Orders from Leeds:", len(leeds))
# Change the test to keep orders with a total over 20 instead.
A common mistake
Warning
Many people think a filter deletes the rows that fail. It does not. The loop builds a new list and the original list keeps every row. So you can filter the same orders again with a different test.
Your turn: big orders
The warehouse wants every order over a set amount, so it can pack them with extra care. Write a function that filters the orders by their total. The hidden tests check your answer.