Engineering Python · Data & Control
List Comprehensions (Light)
In Engineering Python because compact transforms of lists show up in labs — a comprehension is a for-loop written in one expression.
A list comprehension builds a new list by mapping (and optionally filtering) an iterable. Read it as a compact for-loop, not magic.
- Engineering Python
- Medium level
- 4 concepts
1Comprehension ≈ loop
`[expr for x in xs]` is the list of `expr` values for each `x`. It is sugar for an empty list plus append in a for-loop.
Figure. [x * 2 for x in xs] is the list of x * 2 for each x. xs is [1, 2, 3], so the new list is [2, 4, 6] — sugar for an empty list plus append in a for-loop.
Map
xs = [1, 2, 3]
ys = [x * 2 for x in xs]
print(ys)What is `[x+1 for x in [1,2]]`?
- [2, 3]
- [1, 2]
- 3
Each element is incremented into a new list.
2Optional filter
`[expr for x in xs if cond]` keeps only elements where `cond` is True.
Figure. [x for x in xs if cond] keeps only elements where cond is True. xs is [1, 2, 3, 4] and x % 2 == 0 keeps 2 and 4.
Takeaway
- Idea`[expr for x in xs if cond]` keeps only elements where `cond` is True.
Filter
xs = [1, 2, 3, 4]
evens = [x for x in xs if x % 2 == 0]
print(evens)3When to prefer a loop
If the body needs multiple statements or is hard to read on one line, keep a for-loop. Clarity beats cleverness in labs.
Comprehensions shine for map/filter one-liners. Nested side effects, early `break`, or multi-step bookkeeping stay in a loop.
Figure. If the body needs multiple statements or is hard to read on one line, keep a for-loop. Comprehensions shine for map/filter one-liners. Clarity beats cleverness in labs.
Choose the form
- One expressionPrefer a comprehension.
- Several statementsPrefer an explicit for-loop.
When should you keep a plain for-loop instead of a comprehension?
- When the body needs multiple statements or is hard to read on one line
- Whenever the list is longer than 3
- Never — comprehensions always win
Clarity beats clever one-liners.
4Lab: rewrite a loop
Build squares with a comprehension and print them.
Figure. Build [0, 1, 4, 9] with a comprehension over range(4). That is the same result as a loop that appends i*i. The check can see 9.
Takeaway
- IdeaBuild squares with a comprehension and print them.
Coding lab. Squares via comprehension runs in the app, with checks on your output.
Notes
- In Engineering Python because compact transforms of lists show up in labs — a comprehension is a for-loop written in one expression.
- `[expr for x in xs]` is the list of `expr` values for each `x`. It is sugar for an empty list plus append in a for-loop.
- `[expr for x in xs if cond]` keeps only elements where `cond` is True.
Exam traps & shortcuts
- Run one cell at a time and read stdout before changing more lines.
- Names are labels for values; rebinding a name does not rewrite old prints.
Recap
Comprehensions map/filter into a new list; expand to a loop when clarity needs it.
- Comprehension ≈ loop
- `[expr for x in xs]` is the list of `expr` values for each `x`. It is sugar for an empty list plus append in a for-loop.
- Optional filter
- `[expr for x in xs if cond]` keeps only elements where `cond` is True.
- When to prefer a loop
- If the body needs multiple statements or is hard to read on one line, keep a for-loop. Clarity beats cleverness in labs.
- Lab: rewrite a loop
- Build squares with a comprehension and print them.
Practise List Comprehensions (Light)
Reading is free and needs no account. Practice, mocks and progress live in the app.
- A 2-question practice set that ends the chapter
- 2 quick checks with worked explanations
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device