Comprehensions: Practice Questions
100 questions. Try each one yourself before checking the answer.
Short on time? Filter by Must Do for the 25 questions that cover this topic on their own.
Build a list of the squares of 1 to 10 with a loop, then again with a comprehension, and show both give the same answer.
Build a list of the doubles of an existing list.
Build a list of the characters of a word, and a list of their upper-case forms.
Use a method call and a function call inside a comprehension.
Add an if to keep only some of the values.
Filter and transform in the same comprehension.
Use several conditions in a filter.
Use an if/else in the output rather than as a filter.
Show clearly the difference between a filtering if and a conditional expression.
Build a set with a comprehension and show duplicates disappear.
Use a set comprehension with a filter, and combine two of them with set operators.
Build a dictionary with a comprehension.
Build a dictionary from two matching lists, using range(len(...)).
Build a dictionary from an existing one using .items().
Filter a dictionary with a comprehension.
Invert a dictionary with a comprehension.
Use two for clauses in one comprehension to flatten a nested list.
Use two for clauses to build every combination of two lists.
Build a nested list — a comprehension inside a comprehension.
Transpose a matrix with a nested comprehension.
Compare a list comprehension with the generator expression from Topic 17.
Show when the brackets can be dropped entirely.
Use a comprehension over a file, a dictionary and a generator.
Use a helper function inside a comprehension to keep it readable.
Show a comprehension that has gone too far, and the loop that reads better.
Clean a messy list of strings in one comprehension, dropping the empties.
Flatten a nested list with a condition on the inner values.
Build a multiplication table as a nested comprehension and print it.
Transform both the keys and the values of a dictionary.
Build a frequency dictionary with a comprehension, and say why a loop is often better here.
Use set comprehensions to compare two collections.
Use a conditional expression to normalise messy values.
Combine a comprehension with a lambda and a named function.
Extract fields from a list of dictionaries.
Build a dictionary of lists using a comprehension plus a helper.
Generate coordinate pairs and filter them.
Use comprehensions for text processing.
Filter a dictionary in several different ways.
Build and manipulate a matrix entirely with comprehensions.
Use comprehensions over a range with a step, a reversed sequence and a sorted one.
Choose between a comprehension and a generator expression for three different jobs.
Build a lookup table with a comprehension and use it.
Build a list of dictionaries and then reshape it.
Use a comprehension to validate a batch of records and separate good from bad.
Use unpacking in a comprehension's for clause.
Use a set comprehension to find unique derived values.
Build Pascal's triangle with comprehensions.
Use a comprehension to apply several transformations and compare the results.
Use the walrus operator inside a comprehension to avoid computing something twice.
Build a nested dictionary structure with comprehensions.
Process student marks into a full report using comprehensions throughout.
Transform a CSV into records, filter it, and write the result back.
Build several lookup tables from one dataset.
Analyse a block of text with comprehensions.
Normalise and validate a batch of user records.
Build a grade distribution and a bar chart from marks.
Flatten a nested configuration into dotted keys with a comprehension and a helper.
Build an inventory report with comprehensions doing the calculations.
Parse log lines into records with comprehensions and summarise them.
Generate test data with comprehensions.
Build a seating chart and query it with comprehensions.
Merge and reconcile two dictionaries with comprehensions.
Build a simple recommendation report using set comprehensions.
Build a word index mapping each word to the lines it appears on.
Build a prime sieve with comprehensions.
Build a timetable grid and query it.
Convert between data shapes using comprehensions.
Build a currency conversion table with comprehensions.
Build a report combining comprehensions with Counter and defaultdict.
Use comprehensions to compare two datasets and produce a reconciliation report.
Show that a comprehension's loop variable does not leak into the surrounding code.
This flattening comprehension raises NameError. Explain the ordering rule.
grid = [[1, 2], [3, 4]]
flat = [value for value in row for row in grid]This raises SyntaxError. Explain where each kind of if may appear.
numbers = [1, 2, 3, 4, 5, 6]
result = [n for n in numbers if n % 2 == 0 else 0]Show why using a comprehension purely for its side effects is wrong.
Show what happens when a comprehension reads from a generator that has already been used.
Show what happens when a dict comprehension produces duplicate keys.
Take a comprehension that has grown too complex and rewrite it three ways.
Measure when a comprehension is the wrong choice on memory grounds.
Show a comprehension that recomputes the same expensive thing over and over.
Show what a set comprehension silently discards.
Show the difference between two for clauses and a nested comprehension.
Show that a comprehension cannot contain try/except, and give the workaround.
Show that a comprehension can read enclosing variables but cannot assign to them.
Show a comprehension whose readability collapses, and how to name your way out.
Give a clear rule for when a loop beats a comprehension, with examples of each.
Build a Data Cleaning Pipeline where every stage is a comprehension, with a report of what each dropped.
Build a Matrix Toolkit where every operation is a comprehension.
Build a Text Analysis Report using all four comprehension types.
Build a Gradebook Report with comprehensions doing every calculation.
Build a CSV Transformer that reads, reshapes, filters and writes using comprehensions.
Build an Inventory Analyser producing a full report from comprehensions.
Build a Log Report parsing and summarising with comprehensions.
Build a Configuration Processor that flattens, validates and reports on settings.
Build a Sales Dashboard with cross-tabulated comprehensions.
Build a Student Enrolment Analyser using set and dict comprehensions together.
Compare all four comprehension forms side by side, including the generator expression.
Explain how to read and write nested comprehensions reliably.
Give a decision procedure for choosing between a loop, a comprehension and a generator expression.
Explain what comprehensions replaced from earlier topics, and what they cannot replace.
Capstone. Build a Comprehension Toolkit Report using every form: list, set, dict, nested, generator expression, filtering, conditional expressions, unpacking and the walrus operator.
Still stuck on something?
Book a free 1-on-1 session and we'll work through it together.
Book a Free Session