Intermediate · 16 min
Project: expense report
Combine structured data, filtering, sorting, and reusable functions.
Summarize records
Keep calculation code in a function so the same rule works for any list of records.
def total(items):
return sum(item["amount"] for item in items)
expenses = [{"amount": 8}, {"amount": 12}]
print(total(expenses))Output
20
Filter before sorting
A comprehension can select records before sorted() orders them by a chosen key.
items = [{"name": "Train", "amount": 18}, {"name": "Tea", "amount": 3}]
large = [item for item in items if item["amount"] >= 10]
print([item["name"] for item in large])Output
['Train']
Project: expense report
- Read both project: expense report examples before answering.
- Run the task, compare its exact output, and revise the code if needed.
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Build the report
Complete report so it returns the total and the names of expenses costing at least 10, ordered from highest amount to lowest. Display the supplied summary.
Calculate the total separately, then filter and sort the records before returning both values.