Foundation · 50 MINUTES · VERSION 1
Count tasks with Python lists and dictionaries
Build a status report, handle missing values, and practise loops and conditions.
Reviewed 23 Oct 2026Your setup may differ. Windows, Ubuntu, and optional tool bundles →
What you will learn
- Read a list containing dictionaries.
- Use a loop and a condition to count tasks.
- Handle an unknown or missing status without crashing.
Theory: lists, dictionaries, and loops
A list stores items in order. A dictionary stores values under named keys. A task can be a dictionary with keys such as title and status. A list can contain several task dictionaries.
A for loop visits each item. Python uses indentation to group statements. The statements inside a loop or condition must be indented consistently. Use four spaces; avoid mixing tabs and spaces.
This reporting pattern is useful for task dashboards, imported data, and automation scripts. Compared with PHP, Python uses for task in tasks: rather than foreach ($tasks as $task).
Worked example
Create task_report.py:
tasks = [
{"title": "Fix login", "status": "done"},
{"title": "Write tests", "status": "todo"},
{"title": "Review code", "status": "done"},
{"title": "Update notes", "status": "in_progress"},
]
counts = {"todo": 0, "in_progress": 0, "done": 0, "unknown": 0}
allowed_statuses = ("todo", "in_progress", "done")
for task in tasks:
status = task.get("status")
if isinstance(status, str) and status in allowed_statuses:
counts[status] += 1
else:
counts["unknown"] += 1
for status, count in counts.items():
print(f"{status}: {count}")
Run python task_report.py on Windows, py task_report.py if that is your installed launcher, or python3 task_report.py on Ubuntu/Linux. You can also use your virtual environment's interpreter. The same program works across these environments. Expected output:
todo: 1
in_progress: 1
done: 2
unknown: 0
.get("status") returns None when the key is missing. None represents the absence of a value. isinstance(status, str) checks for a string. The tuple allowed_statuses lists the supported values. += 1 increases the chosen count by one.
This example assumes every task is a dictionary. Validating the type of every imported row is a separate exercise. For n tasks, the counting loop takes O(n) time. The four counters use constant additional space. The input list itself still grows with the number of tasks.
Guided practice
- Run the unchanged example and compare all four output lines.
- Add another
todoitem. Expecttodo: 2. - Add a task with status
blocked. Expectunknown: 1. - Add a dictionary without
status. It should add one more unknown task. - Use an empty list. All counters should be zero.
- Try a list as the status value. The string type check should direct it to
unknown.
Independent exercise
Create count_tasks(tasks) as a function that returns the dictionary instead of printing. Keep printing outside the function. Test an empty input, all-done input, mixed statuses, missing status, and a non-string status. Do not modify the input dictionaries.
This introduces functions: def count_tasks(tasks): starts the function, and return counts returns the result. The next planned module explores functions in depth.
Common mistakes
- Creating
countsinside the loop resets the report for every task. - A missing colon after
fororifcauses a syntax error. - Incorrect indentation can put a statement in the wrong block.
- Accessing
task["status"]directly raisesKeyErrorif the key is absent.
Self-check — not graded
- What should an empty task list produce?
- Why use
.get()here? - Why return the report from a function rather than always printing it?
Interview practice
“Should an application fetch a million MySQL rows just to count statuses?” Consider doing aggregation in SQL with GROUP BY, and measure the query. Python is useful when the task really needs local data processing; it is not a reason to move every database calculation into a script.
English practice and glossary
- List: an ordered collection of items.
- Dictionary: key/value data.
- Loop: a repeated operation over items.
- Indentation: leading spaces that define Python blocks.
- Fallback: behavior used when expected data is absent or unsupported.
Say: “This script counts tasks by status. It keeps unknown values separate. I tested it with an empty list and a missing status.” Write your own short report of the changes you made.
Reveal exercise hints and self-check answers
Function hint: initialize counts inside the function but before the loop. Return it after the counting loop. The display code can call .items() on the returned dictionary.
- Four counters, all zero.
- It provides a missing-value result instead of raising an error for an absent key.
- Other code can reuse, display, and test the returned data without capturing terminal output.