Filtering and Transforming
Map Transform
Apply a formula to every element of a list, collecting the results in a new
list. Here, Celsius readings are converted to Fahrenheit. The trace shows
the intermediate result f computed on each iteration before it is appended.
By hand
Compute f = c * 9 / 5 + 32 inside the loop and append it to fahrenheit.
The intermediate variable f makes each conversion visible as a separate
step in the trace.
naive.py
Replay: real traced execution (multi-file project)
celsius = [0, 20, 37, 100, -10, 25]
fahrenheit = []
for c in celsius:
f = c * 9 / 5 + 32
fahrenheit.append(f)
print('RESULT:', fahrenheit)
celsius ← [0, 20, 37, 100, -10, 25]
1celsius = [0, 20, 37, 100, -10, 25]2fahrenheit = []values this step[0, 20, 37, 100, -10, 25]celsiusfahrenheit ← []
1celsius = [0, 20, 37, 100, -10, 25]2fahrenheit = []3for c in celsius:values this step[]fahrenheitc ← 0
2fahrenheit = []3for c in celsius:4 f = c * 9 / 5 + 32values this step0cf ← 32.0
3for c in celsius:4 f = c * 9 / 5 + 325 fahrenheit.append(f)values this step32.0ffahrenheit ← [32.0]
4 f = c * 9 / 5 + 325 fahrenheit.append(f)6print('RESULT:', fahrenheit)values this step[] → [32.0]fahrenheitc ← 20
2fahrenheit = []3for c in celsius:4 f = c * 9 / 5 + 32values this step0 → 20cf ← 68.0
3for c in celsius:4 f = c * 9 / 5 + 325 fahrenheit.append(f)values this step32.0 → 68.0ffahrenheit ← [32.0, 68.0]
4 f = c * 9 / 5 + 325 fahrenheit.append(f)6print('RESULT:', fahrenheit)values this step[32.0] → [32.0, 68.0]fahrenheitc ← 37
2fahrenheit = []3for c in celsius:4 f = c * 9 / 5 + 32values this step20 → 37cf ← 98.6
3for c in celsius:4 f = c * 9 / 5 + 325 fahrenheit.append(f)values this step68.0 → 98.6ffahrenheit ← [32.0, 68.0, 98.6]
4 f = c * 9 / 5 + 325 fahrenheit.append(f)6print('RESULT:', fahrenheit)values this step[32.0, 68.0] → [32.0, 68.0, 98.6]fahrenheitc ← 100
2fahrenheit = []3for c in celsius:4 f = c * 9 / 5 + 32values this step37 → 100cf ← 212.0
3for c in celsius:4 f = c * 9 / 5 + 325 fahrenheit.append(f)values this step98.6 → 212.0ffahrenheit ← [32.0, 68.0, 98.6, 212.0]
4 f = c * 9 / 5 + 325 fahrenheit.append(f)6print('RESULT:', fahrenheit)values this step[32.0, 68.0, 98.6] → [32.0, 68.0, 98.6, 212.0]fahrenheitc ← -10
2fahrenheit = []3for c in celsius:4 f = c * 9 / 5 + 32values this step100 → -10cf ← 14.0
3for c in celsius:4 f = c * 9 / 5 + 325 fahrenheit.append(f)values this step212.0 → 14.0ffahrenheit ← [32.0, 68.0, 98.6, 212.0, 14.0]
4 f = c * 9 / 5 + 325 fahrenheit.append(f)6print('RESULT:', fahrenheit)values this step[32.0, 68.0, 98.6, 212.0] → [32.0, 68.0, 98.6, 212.0, 14.0]fahrenheitc ← 25
2fahrenheit = []3for c in celsius:4 f = c * 9 / 5 + 32values this step-10 → 25cf ← 77.0
3for c in celsius:4 f = c * 9 / 5 + 325 fahrenheit.append(f)values this step14.0 → 77.0ffahrenheit ← [32.0, 68.0, 98.6, 212.0, 14.0, 77.0]
4 f = c * 9 / 5 + 325 fahrenheit.append(f)6print('RESULT:', fahrenheit)values this step[32.0, 68.0, 98.6, 212.0, 14.0] → [32.0, 68.0, 98.6, 212.0, 14.0, 77.0]fahrenheitfor c in celsius:
2fahrenheit = []3for c in celsius:4 f = c * 9 / 5 + 32stdout ← RESULT: [32.0, 68.0, 98.6, 212.0, 14.0, 77.0]
5 fahrenheit.append(f)6print('RESULT:', fahrenheit)values this stepRESULT: [32.0, 68.0, 98.6, 212.0, 14.0, 77.0]stdout
The Pythonic way
A list comprehension [c * 9 / 5 + 32 for c in celsius] applies the formula
to every element in one expression, with no accumulator or append call.
library.py
celsius = [0, 20, 37, 100, -10, 25]
fahrenheit = [c * 9 / 5 + 32 for c in celsius]
print('RESULT:', fahrenheit)
RESULT: [32.0, 68.0, 98.6, 212.0, 14.0, 77.0]
Implementation notes
- The chosen values all produce clean decimal results; no rounding is needed at the print boundary.
list(map(lambda c: c * 9 / 5 + 32, celsius))is an equivalent functional form; the list comprehension is preferred in modern Python for readability.- The comprehension runs in its own scope, so neither
cnor the intermediate result appears as a traced variable in the library half.