Lab: filter and report
Read a simulated dataset, apply boolean filters, manipulate strings, and write a formatted report.
- Apply boolean conditions to filter records from a list
- Extract and transform fields using string methods
- Format and write results to simulate file output
This is an optional lab. No new concepts — just hands-on practice combining boolean logic, string methods, and file I/O on a realistic dataset. Work through each checkpoint, run the checks, and carry the patterns with you.
Real data pipelines follow a simple shape: read records, filter by some condition, transform the fields, and write the result. That's what you'll build here, step by step. The dataset is a Python list of strings so you can run everything in the browser — but every pattern maps directly to reading lines from a file.
The dataset
Each record is a comma-separated string with three fields: name, department,
and level. This is the kind of thing you'd read line-by-line from a CSV file
using the techniques in the file I/O lesson.
Notice the pattern: split(",") gives you [name, department, level] at indices
0, 1, and 2. Every checkpoint builds on that.
Checkpoint 1 — count matching records
The simplest filter: count how many records belong to a given department. Use boolean logic to check the department field, and sum the matches.
Write count_matching(records, department) that returns the number of records whose department field equals the given department string.
count_matching(RECORDS, "eng") → 3count_matching(RECORDS, "finance") → 0The clean one-liner uses a generator expression inside sum:
sum(1 for r in records if r.split(",")[1] == department). A plain for loop
with a counter is equally correct — use whichever feels more readable to you.
Checkpoint 2 — extract names
Now collect which names match, not just how many. Return a list of name strings for all records in the given department.
Write extract_names(records, department) that returns a list of the name strings (index 0) for records whose department field matches.
extract_names(RECORDS, "eng") → ["alice", "carol", "dave"]extract_names(RECORDS, "finance") → []Checkpoint 3 — format a report
Now bring string methods in. Transform each matching record into a formatted
report line: capitalize the name, include the level in parentheses. The result
for an eng filter should look like ["Alice (senior)", "Carol (junior)", "Dave (senior)"].
Write format_report_lines(records, department) that returns formatted strings for matching records. Each line: "{Capitalized name} ({level})" e.g. "Alice (senior)".
format_report_lines(RECORDS, "eng") → ["Alice (senior)", "Carol (junior)", "Dave (senior)"]Use parts[0].capitalize() (or .title()) for the name and an f-string to
assemble the line: f"{name} ({level})". That's the string-methods lesson and
the file-I/O lesson working together — in a real program you'd then write those
lines to a file with f.write(line + "\n").
Done?
Three green checkpoints means you can take raw string records, apply boolean filters, transform the fields, and produce formatted output. That pipeline — read, filter, transform, write — is the backbone of an enormous amount of real Python work. You're ready for the intermediate track, where list comprehensions and generators make this style even more concise.