Your next chapter / Count tasks with Python lists and dictionaries

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 2026

Your setup may differ. Windows, Ubuntu, and optional tool bundles →

Log in or create an account to keep private notes, bookmarks, and learning progress. Reading is free without an account.

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

  1. Run the unchanged example and compare all four output lines.
  2. Add another todo item. Expect todo: 2.
  3. Add a task with status blocked. Expect unknown: 1.
  4. Add a dictionary without status. It should add one more unknown task.
  5. Use an empty list. All counters should be zero.
  6. 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 counts inside the loop resets the report for every task.
  • A missing colon after for or if causes a syntax error.
  • Incorrect indentation can put a statement in the wrong block.
  • Accessing task["status"] directly raises KeyError if the key is absent.

Self-check — not graded

  1. What should an empty task list produce?
  2. Why use .get() here?
  3. 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.

  1. Four counters, all zero.
  2. It provides a missing-value result instead of raising an error for an absent key.
  3. Other code can reuse, display, and test the returned data without capturing terminal output.

Official references

Practise on your own computer. The exercise self-checks are not graded. This version has no scored quiz. Signed-in learners can save reading, self-reported practice, private notes, and bookmarks.