Practice Bank: Comprehensions, Lambda & Functional Tools
A deeper set of practice problems, organized by topic, for extra reps
beyond the main exercises — including scenario-based problems
written in the same style you'll meet in real interviews. Uses
everything from Chapters 1-8 plus this chapter's own toolkit: list
comprehensions (with and without a filter if), dict and
set comprehensions, nested comprehensions, lambda
functions, and the functional tools map(),
filter(), and sorted()/.sort()
with key=lambda. No def (that's Chapter
10), try/except, file I/O, classes, or
imports yet.
starter.py below, then run it from a terminal
with python3 starter.py.
Topic 1: List Comprehensions
1 Basic comprehension vs. for loop
Find # TODO 1.1. Given numbers = [1, 2, 3, 4, 5], build a list of squares with a plain for loop and .append(), then build the same list with a list comprehension. Print both.
2 Filtering with if
Find # TODO 1.2. Use a list comprehension with an if filter to keep only the even numbers from a list of 1-10.
3 Filtering strings by length
Find # TODO 1.3. Use a list comprehension with an if filter to keep only words longer than 3 letters.
4 Transform + filter together
Find # TODO 1.4. Combine a transform and a filter in one comprehension: square only the even numbers.
5 Debug the Code
Find # TODO 1.5. This comprehension is missing the expression before the for clause and raises a SyntaxError. Fix it.
Cleaning form input
Find # TODO 1.A. Use a list comprehension with .strip() to clean whitespace from a list of raw form entries.
Why comprehensions over manual loops?
Find # TODO 1.B. Build a doubled-prices list with a comprehension, and explain in a comment why it beats a manual for loop with .append() for simple transforms.
Topic 2: Dict & Set Comprehensions
1 Dict comprehension: word lengths
Find # TODO 2.1. Build a dict comprehension mapping each word to its length.
2 Dict comprehension with a filter
Find # TODO 2.2. Use a dict comprehension with an if filter to keep only passing scores (60 or higher).
3 Set comprehension: unique lengths
Find # TODO 2.3. Use a set comprehension to collect the distinct word lengths that appear in a list.
4 Dict comprehension from parallel lists
Find # TODO 2.4. Use a dict comprehension over range(len(...)) to combine two parallel lists into one dict.
5 Debug the Code
Find # TODO 2.5. This uses square brackets instead of curly braces with a colon, so it doesn't build a dict comprehension. Fix it.
Price lookup table
Find # TODO 2.A. Build a dict comprehension mapping product names to prices from two parallel lists.
Set comprehension vs. list comprehension
Find # TODO 2.B. Pull unique email domains with a set comprehension, and explain in a comment when a set comprehension beats a list comprehension.
Topic 3: Nested Comprehensions & Readability Limits
1 Flattening a list of lists
Find # TODO 3.1. Use a nested comprehension (for row in nested for value in row) to flatten a list of lists.
2 Multiplication table
Find # TODO 3.2. Build a 3x3 multiplication table as a list of lists using a nested comprehension.
3 Nested comprehension with a filter
Find # TODO 3.3. Flatten only the even numbers out of a list of lists using a nested comprehension with an if.
4 Debug the Code
Find # TODO 3.4. This nested comprehension has its for clauses in the wrong order, raising a NameError. Fix the order.
5 Comprehension vs. readable loop
Find # TODO 3.5. Rewrite a flattening comprehension as an equivalent nested for loop, and comment on which reads better.
Flattening nested survey data
Find # TODO 3.A. Flatten a list of per-department name lists into one flat list of names.
When nesting hurts readability
Find # TODO 3.B. Flatten a matrix with a nested comprehension, and explain in a comment why deep nesting is usually a readability smell in production code.
Topic 4: Lambda Functions
1 A simple lambda
Find # TODO 4.1. Write a lambda that doubles a number and call it directly.
2 Lambda with two arguments
Find # TODO 4.2. Write a lambda that takes two arguments and returns their sum.
3 Storing and reusing a lambda
Find # TODO 4.3. Store a squaring lambda in a variable and call it with several different arguments.
4 Conditional expression in a lambda
Find # TODO 4.4. Use a conditional expression inside a lambda to return "even" or "odd".
5 Debug the Code
Find # TODO 4.5. This lambda incorrectly uses a return statement, raising a SyntaxError. Fix it.
Quick one-off formatter
Find # TODO 4.A. Write a lambda that formats a price as a currency string.
Lambda's single-expression limit
Find # TODO 4.B. Write a simple lambda, then explain in a comment the single-expression limitation of lambda and when you'd use def instead.
Topic 5: map(), filter(), and sorted() with key=lambda
1 map() with a lambda
Find # TODO 5.1. Use map() with a lambda to convert a list of strings to their lengths.
2 filter() with a lambda
Find # TODO 5.2. Use filter() with a lambda to keep only positive numbers.
3 sorted() with key=lambda
Find # TODO 5.3. Use sorted() with key=lambda to sort a list of tuples by the second element.
4 .sort() in place with key=lambda
Find # TODO 5.4. Use .sort() with key=lambda to sort a list of dicts by one field, in place.
5 Debug the Code
Find # TODO 5.5. This sorted(..., key=lambda) call references the wrong field name, raising a KeyError. Fix it.
Sorting employee records
Find # TODO 5.A. Sort a list of employee dicts by salary, highest first, using sorted() and key=lambda.
Why wrap map()/filter() in list()?
Find # TODO 5.B. Print a raw map() object, then its list()-wrapped version, and explain in a comment why the wrapping is necessary.
Topic 6: Real-World Comprehensions & Functional Tools in Production
1 Cleaning raw log lines
Find # TODO 6.1. Use a list comprehension to strip whitespace and drop empty lines from a batch of raw log lines.
2 Building a lookup dict from records
Find # TODO 6.2. Build a lookup dictionary from a list of record dicts using a dict comprehension.
3 filter() + comprehension pipeline
Find # TODO 6.3. Combine filter() and a list comprehension to validate and convert a batch of user-submitted ages.
4 Debug the Code
Find # TODO 6.4. This pipeline filters strings before converting them to integers, raising a TypeError. Fix the order.
5 Ranking with sorted()
Find # TODO 6.5. Use sorted() with key=lambda to rank a list of products by a computed score.
Deduplicating and sorting API results
Find # TODO 6.A. Deduplicate a raw list of API results with a set comprehension, then sort the result.
Why these tools dominate data-cleaning pipelines
Find # TODO 6.B. Clean and rank a batch of raw scores, then explain in a comment why comprehensions and functional tools are so common in real data-cleaning pipelines.
solution.py. Your exact
wording doesn't need to match — the goal is that your program
runs without errors and does what each TODO asks.