Understand the idea
A problem you can recognise
You have a list of session durations and need another list with each duration doubled. A loop can build it correctly. A comprehension can express the same transformation compactly, but only if you can still explain where every output value came from.
The aim is not to make the code as short as possible. It is to make a small collection transformation easy to read and check.
Understand the idea
Understand the idea
A comprehension constructs a collection from an iterable using an expression and optional filtering conditions. Read a simple list comprehension as: evaluate the leading expression for each item that passes the filter. It is a compact collection-building tool, not a requirement to replace every loop.
The expression determines what enters the result; the filter determines whether an input participates. These roles are separate. An if after the iterable filters items, while a conditional expression before for chooses between output values. Start with a normal loop if the distinction is not yet clear. Name the source collection and result so the transformation can be explained.
List, set and dictionary comprehensions build different collection types. A dictionary comprehension creates key-value associations, and duplicate keys overwrite earlier values just as in other dictionary construction. Avoid side effects inside a comprehension. If the code is mainly printing, updating unrelated state or handling several branches, an explicit loop usually communicates the intent better. Compact syntax is useful only when the reader can still reason about the operation.
See how it works
Example 1: Transform each input
PYTHON exampleRead the code, then try a change.
Try this example
minutes = [10, 20, 30]
doubled = [value * 2 for value in minutes]
print(doubled)
print(minutes)
Expected output:
ReferenceCompare this with the explanation.
[20, 40, 60]
[10, 20, 30]
The expression multiplies each number before it enters the new list. The original list is not changed by this construction.
See how it works
Example 2: Filter before collecting
PYTHON exampleRead the code, then try a change.
Try this example
scores = [65, 80, 92]
passed = [score for score in scores if score >= 80]
print(passed)
Expected output:
ReferenceCompare this with the explanation.
[80, 92]
Only values satisfying the inclusive passing condition enter the result. The leading expression is score, so the included numeric values are retained without another transformation.
Your turn
Example 3: Build named associations
PYTHON exampleRead the code, then try a change.
Try this example
subjects = ["HTML", "Python"]
lengths = {subject: len(subject) for subject in subjects}
print(lengths["HTML"])
print(lengths["Python"])
Expected output:
ReferenceCompare this with the explanation.
4
6
The comprehension produces a dictionary, not a list. Each subject becomes a key associated with its character count, and the prints perform explicit key lookups.
Understand the idea
Think through the important decisions
Read a list comprehension in a deliberate order: locate the source collection, identify the loop variable, inspect any filter, and finally inspect the expression that constructs each output item. This reading order follows the work even though the output expression is written first.
Distinguish transforming from filtering. Doubling each duration preserves the number of source entries. Keeping only durations above a threshold can reduce the count. A comprehension that does both should let you explain why each source entry was included or excluded before discussing its transformed value.
Compare the comprehension with an ordinary loop that appends values to a new list. If you cannot write or explain that loop, the short expression may be hiding a gap in understanding. Test an empty source collection too: constructing an empty result should be a normal outcome, not a special surprise.
Check your understanding
Check the reasoning
The comprehension expression transforms each included item; its trailing condition filters inputs.
A matching screenshot or output is one check, not proof that a program is correct for all inputs. Change one input, predict the new result and run again. If the result differs, find the first instruction where your prediction stops matching the program state. Keep that small case as a regression check when you fix the cause.
Spot the mistake
The mistake worth understanding
Nested comprehensions, side effects and several conditions can become difficult to follow. Shorter source is not proof of clearer source. Use an ordinary loop when named intermediate steps, early exits or explicit error handling make the operation easier to understand.
Your turn
Independent practice
From durations [10, 25, 40, 15], build a new list containing double the durations that are at least 20. Then write the equivalent ordinary loop and compare the results.
Your turn
Practice hint
Transform with value * 2 and filter with value >= 20. Do not reverse those roles.
Change it and explain it
Experiment with the first program From durations [10, 25, 40, 15], build a new list containing double the durations that are at least 20. Then write the equivalent ordinary loop and compare the results. Start by changing one input in this smaller example. Loading it does not execute it.
minutes = [10, 20, 30]
doubled = [value * 2 for value in minutes]
print(doubled)
print(minutes) Open in Practice Loading an example does not run it. Your existing edits are protected by a replacement confirmation.
Your turn
Try a different case
Explain one included input, one excluded input and the empty-input case for your filtered example. Then state whether the original collection changes. A newly constructed list is not an instruction to mutate the source.