Summarize Transformed Values
Feed transformed values directly into sum, any, or all when the program does not need to inspect an intermediate result list.
The previous lesson built a complete corrected list before summarizing it:
Keeping corrected is useful when we want to print, inspect, or reuse its
values. Sometimes the program needs only the final summary.
Send Each Produced Value into sum
Place the same comprehension form inside sum, but omit the list brackets:
total = sum(reading + 0.5 for reading in readings)
The expression inside sum is a generator expression. For this call,
Python produces one corrected value at a time and gives it directly to sum.
It does not build a separate corrected list.
Read from the for clause just as before:
- take each
readingfromreadings; - calculate
reading + 0.5; - give that value to
sum.
Both programs return 84.0. The choice is about whether the intermediate values deserve a name and inspection.
Q1. Read values consumed by sum
What does this expression return?
sum(reading * 2 for reading in [3, 4, 5])
Compute it first, then check your number.
HintMake the hidden sequence visible on paper
Write 6, 8, and 10 before calculating the total.
SolutionTransform, then summarize
The expression produces 6, 8, and 10 in order. sum returns 24.
Filter before Sending Values to the Summary
The None filter remains in the same position:
The generator produces 18 and 22, so sum returns 40. If the order feels
unclear, rebuild the visible list first:
The named list is preferable when seeing available would help us test or
debug the program.
Use the Form Only inside a Known Summary
In this chapter, keep generator expressions inside sum, any, or all:
The first produces true-or-false values until one is false. The second produces them until one is true. We are not storing a generator in a name or studying iterator machinery. If the intermediate decisions matter, build a named list or write the ordinary loop.
Q2. Expand a generator expression
Which list shows the values supplied to sum?
sum(reading - 2 for reading in [10, 14, 12])
Select one choice, then check.
HintSeparate production from reduction
First transform every input. sum combines the produced values afterward.
SolutionThe generator produces three adjusted values
Its visible list equivalent is [reading - 2 for reading in [10, 14, 12]],
which produces [8, 12, 10].
Q3. Remove an unneeded intermediate list
Rewrite the calculation with a generator expression inside sum. The program
should still display 42.
Editable Python
Ready to run.
HintKeep the comprehension's reading order
Replace the two calculation lines with
total = sum(reading + 1 for reading in readings).
SolutionFeed transformed values to sum
total = sum(reading + 1 for reading in readings)
A generator expression inside a known summary avoids an intermediate list that no one needs to inspect. We can always expand it back into a list or loop. The final lesson will order labelled results without changing their source list.