Python · Functional Python

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.

Q1List ComprehensionsEasyMust Do

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.

Q2List ComprehensionsEasy

Build a list of the doubles of an existing list.

Q3List ComprehensionsEasy

Build a list of the characters of a word, and a list of their upper-case forms.

Q4List ComprehensionsEasy

Use a method call and a function call inside a comprehension.

Q5FilteringEasyMust Do

Add an if to keep only some of the values.

Q6FilteringEasy

Filter and transform in the same comprehension.

Q7FilteringEasy

Use several conditions in a filter.

Q8Conditional ExpressionsEasyMust Do

Use an if/else in the output rather than as a filter.

Q9Conditional ExpressionsEasyMust Do

Show clearly the difference between a filtering if and a conditional expression.

Q10Set ComprehensionsEasyMust Do

Build a set with a comprehension and show duplicates disappear.

Q11Set ComprehensionsEasy

Use a set comprehension with a filter, and combine two of them with set operators.

Q12Dict ComprehensionsEasyMust Do

Build a dictionary with a comprehension.

Q13Dict ComprehensionsEasy

Build a dictionary from two matching lists, using range(len(...)).

Q14Dict ComprehensionsEasy

Build a dictionary from an existing one using .items().

Q15Dict ComprehensionsEasy

Filter a dictionary with a comprehension.

Q16Dict ComprehensionsEasy

Invert a dictionary with a comprehension.

Q17Nested LoopsEasyMust Do

Use two for clauses in one comprehension to flatten a nested list.

Q18Nested LoopsEasy

Use two for clauses to build every combination of two lists.

Q19Nested ComprehensionsEasyMust Do

Build a nested list — a comprehension inside a comprehension.

Q20Nested ComprehensionsEasy

Transpose a matrix with a nested comprehension.

Q21Comprehension vs GeneratorEasy

Compare a list comprehension with the generator expression from Topic 17.

Q22Comprehension vs GeneratorEasy

Show when the brackets can be dropped entirely.

Q23Comprehensions over IterablesEasy

Use a comprehension over a file, a dictionary and a generator.

Q24Comprehensions with FunctionsEasy

Use a helper function inside a comprehension to keep it readable.

Q25ReadabilityEasy

Show a comprehension that has gone too far, and the loop that reads better.

Q26List ComprehensionsMediumMust Do

Clean a messy list of strings in one comprehension, dropping the empties.

Q27Nested LoopsMedium

Flatten a nested list with a condition on the inner values.

Q28Nested ComprehensionsMedium

Build a multiplication table as a nested comprehension and print it.

Q29Dict ComprehensionsMediumMust Do

Transform both the keys and the values of a dictionary.

Q30Dict ComprehensionsMedium

Build a frequency dictionary with a comprehension, and say why a loop is often better here.

Q31Set ComprehensionsMedium

Use set comprehensions to compare two collections.

Q32Conditional ExpressionsMedium

Use a conditional expression to normalise messy values.

Q33Comprehensions with FunctionsMedium

Combine a comprehension with a lambda and a named function.

Q34List ComprehensionsMediumMust Do

Extract fields from a list of dictionaries.

Q35Dict ComprehensionsMedium

Build a dictionary of lists using a comprehension plus a helper.

Q36Nested ComprehensionsMedium

Generate coordinate pairs and filter them.

Q37StringsMedium

Use comprehensions for text processing.

Q38FilteringMediumMust Do

Filter a dictionary in several different ways.

Q39Nested ComprehensionsMedium

Build and manipulate a matrix entirely with comprehensions.

Q40Comprehensions over IterablesMedium

Use comprehensions over a range with a step, a reversed sequence and a sorted one.

Q41Comprehension vs GeneratorMediumMust Do

Choose between a comprehension and a generator expression for three different jobs.

Q42Dict ComprehensionsMedium

Build a lookup table with a comprehension and use it.

Q43List ComprehensionsMedium

Build a list of dictionaries and then reshape it.

Q44FilteringMedium

Use a comprehension to validate a batch of records and separate good from bad.

Q45Nested LoopsMediumMust Do

Use unpacking in a comprehension's for clause.

Q46Set ComprehensionsMedium

Use a set comprehension to find unique derived values.

Q47Nested ComprehensionsMedium

Build Pascal's triangle with comprehensions.

Q48Comprehensions with FunctionsMedium

Use a comprehension to apply several transformations and compare the results.

Q49Walrus OperatorMedium

Use the walrus operator inside a comprehension to avoid computing something twice.

Q50Nested ComprehensionsMedium

Build a nested dictionary structure with comprehensions.

Q51Real-WorldMediumMust Do

Process student marks into a full report using comprehensions throughout.

Q52Real-WorldMediumMust Do

Transform a CSV into records, filter it, and write the result back.

Q53Real-WorldMedium

Build several lookup tables from one dataset.

Q54StringsMediumMust Do

Analyse a block of text with comprehensions.

Q55Real-WorldMedium

Normalise and validate a batch of user records.

Q56Real-WorldMedium

Build a grade distribution and a bar chart from marks.

Q57Comprehensions with FunctionsMedium

Flatten a nested configuration into dotted keys with a comprehension and a helper.

Q58Real-WorldMediumMust Do

Build an inventory report with comprehensions doing the calculations.

Q59Real-WorldMedium

Parse log lines into records with comprehensions and summarise them.

Q60Comprehensions over IterablesMedium

Generate test data with comprehensions.

Q61Nested ComprehensionsMedium

Build a seating chart and query it with comprehensions.

Q62Dict ComprehensionsMediumMust Do

Merge and reconcile two dictionaries with comprehensions.

Q63Real-WorldMedium

Build a simple recommendation report using set comprehensions.

Q64Real-WorldMedium

Build a word index mapping each word to the lines it appears on.

Q65Nested ComprehensionsMedium

Build a prime sieve with comprehensions.

Q66Real-WorldMedium

Build a timetable grid and query it.

Q67Real-WorldMedium

Convert between data shapes using comprehensions.

Q68Real-WorldMedium

Build a currency conversion table with comprehensions.

Q69Real-WorldMedium

Build a report combining comprehensions with Counter and defaultdict.

Q70Real-WorldMedium

Use comprehensions to compare two datasets and produce a reconciliation report.

Q71ScopeHardMust Do

Show that a comprehension's loop variable does not leak into the surrounding code.

Q72Nested LoopsHard

This flattening comprehension raises NameError. Explain the ordering rule.

grid = [[1, 2], [3, 4]]
flat = [value for value in row for row in grid]
Q73Conditional ExpressionsHardMust Do

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]
Q74Anti-PatternsHard

Show why using a comprehension purely for its side effects is wrong.

Q75Comprehension vs GeneratorHard

Show what happens when a comprehension reads from a generator that has already been used.

Q76Dict ComprehensionsHard

Show what happens when a dict comprehension produces duplicate keys.

Q77ReadabilityHardMust Do

Take a comprehension that has grown too complex and rewrite it three ways.

Q78MemoryHard

Measure when a comprehension is the wrong choice on memory grounds.

Q79Anti-PatternsHard

Show a comprehension that recomputes the same expensive thing over and over.

Q80Set ComprehensionsHard

Show what a set comprehension silently discards.

Q81Nested ComprehensionsHard

Show the difference between two for clauses and a nested comprehension.

Q82Comprehensions with FunctionsHard

Show that a comprehension cannot contain try/except, and give the workaround.

Q83ScopeHard

Show that a comprehension can read enclosing variables but cannot assign to them.

Q84Anti-PatternsHard

Show a comprehension whose readability collapses, and how to name your way out.

Q85DesignHard

Give a clear rule for when a loop beats a comprehension, with examples of each.

Q86Mini-ProjectMini-ProjectMust Do

Build a Data Cleaning Pipeline where every stage is a comprehension, with a report of what each dropped.

Q87Mini-ProjectMini-Project

Build a Matrix Toolkit where every operation is a comprehension.

Q88Mini-ProjectMini-Project

Build a Text Analysis Report using all four comprehension types.

Q89Mini-ProjectMini-ProjectMust Do

Build a Gradebook Report with comprehensions doing every calculation.

Q90Mini-ProjectMini-Project

Build a CSV Transformer that reads, reshapes, filters and writes using comprehensions.

Q91Mini-ProjectMini-Project

Build an Inventory Analyser producing a full report from comprehensions.

Q92Mini-ProjectMini-Project

Build a Log Report parsing and summarising with comprehensions.

Q93Mini-ProjectMini-Project

Build a Configuration Processor that flattens, validates and reports on settings.

Q94Mini-ProjectMini-Project

Build a Sales Dashboard with cross-tabulated comprehensions.

Q95Mini-ProjectMini-Project

Build a Student Enrolment Analyser using set and dict comprehensions together.

Q96InterviewInterview

Compare all four comprehension forms side by side, including the generator expression.

Q97Nested ComprehensionsInterview

Explain how to read and write nested comprehensions reliably.

Q98DesignInterview

Give a decision procedure for choosing between a loop, a comprehension and a generator expression.

Q99DesignInterview

Explain what comprehensions replaced from earlier topics, and what they cannot replace.

Q100CapstoneInterviewMust Do

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