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)
  1. celsius ← [0, 20, 37, 100, -10, 25]

    1celsius = [0, 20, 37, 100, -10, 25]2fahrenheit = []
    values this step[0, 20, 37, 100, -10, 25]celsius
  2. fahrenheit ← []

    1celsius = [0, 20, 37, 100, -10, 25]2fahrenheit = []3for c in celsius:
    values this step[]fahrenheit
  3. c ← 0

    2fahrenheit = []3for c in celsius:4    f = c * 9 / 5 + 32
    values this step0c
  4. f ← 32.0

    3for c in celsius:4    f = c * 9 / 5 + 325    fahrenheit.append(f)
    values this step32.0f
  5. fahrenheit ← [32.0]

    4    f = c * 9 / 5 + 325    fahrenheit.append(f)6print('RESULT:', fahrenheit)
    values this step[] [32.0]fahrenheit
  6. c ← 20

    2fahrenheit = []3for c in celsius:4    f = c * 9 / 5 + 32
    values this step0 20c
  7. f ← 68.0

    3for c in celsius:4    f = c * 9 / 5 + 325    fahrenheit.append(f)
    values this step32.0 68.0f
  8. fahrenheit ← [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]fahrenheit
  9. c ← 37

    2fahrenheit = []3for c in celsius:4    f = c * 9 / 5 + 32
    values this step20 37c
  10. f ← 98.6

    3for c in celsius:4    f = c * 9 / 5 + 325    fahrenheit.append(f)
    values this step68.0 98.6f
  11. fahrenheit ← [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]fahrenheit
  12. c ← 100

    2fahrenheit = []3for c in celsius:4    f = c * 9 / 5 + 32
    values this step37 100c
  13. f ← 212.0

    3for c in celsius:4    f = c * 9 / 5 + 325    fahrenheit.append(f)
    values this step98.6 212.0f
  14. fahrenheit ← [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]fahrenheit
  15. c ← -10

    2fahrenheit = []3for c in celsius:4    f = c * 9 / 5 + 32
    values this step100 -10c
  16. f ← 14.0

    3for c in celsius:4    f = c * 9 / 5 + 325    fahrenheit.append(f)
    values this step212.0 14.0f
  17. fahrenheit ← [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]fahrenheit
  18. c ← 25

    2fahrenheit = []3for c in celsius:4    f = c * 9 / 5 + 32
    values this step-10 25c
  19. f ← 77.0

    3for c in celsius:4    f = c * 9 / 5 + 325    fahrenheit.append(f)
    values this step14.0 77.0f
  20. fahrenheit ← [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]fahrenheit
  21. for c in celsius:

    2fahrenheit = []3for c in celsius:4    f = c * 9 / 5 + 32
  22. stdout ← 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 c nor the intermediate result appears as a traced variable in the library half.