Build and Change Lists
Begin with an empty result, append accepted readings in order, and distinguish a changed list from the None returned by a mutating method.
The measurement program can inspect existing readings. It may also need to collect a new list containing only the readings it accepts.
readings = [18, None, 21, 20, None, 23]
Begin with an Empty Result
An empty pair of square brackets creates a list with no items:
accepted_readings = []
The list is the result being built. Visit each source reading and append only an available one:
The output is:
[18, 21, 20, 23]
append adds one item to the end of the existing list. The order of the
accepted readings remains the order in which the loop visited them. Repeated
values would remain repeated; this loop filters unavailable readings but does
not remove duplicates.
Trace the visible change after each accepted item:
| Source reading | Action | accepted_readings after the action |
|---|---|---|
| 18 | append | [18] |
None | skip | [18] |
| 21 | append | [18, 21] |
| 20 | append | [18, 21, 20] |
None | skip | [18, 21, 20] |
| 23 | append | [18, 21, 20, 23] |
Q1. Trace a growing result list
What is kept after this loop ends?
Select one choice, then check.
HintDo not remove duplicates
The condition filters only None.
SolutionKeep three readings
The loop appends 12, then 15, then the second 12, producing [12, 15, 12].
Mutation Changes the Existing List
append is a mutating method: it changes the list on which it is called.
The name continues to refer to that changed list:
The method performs a change rather than calculating a replacement list. Its
return value is None:
This distinction matters when reading an assignment:
accepted_readings = accepted_readings.append(23)
The method first changes the list. It then returns None, and the assignment
makes accepted_readings refer to that None result. The name no longer gives
access to the changed list. Call append on its own line instead.
Q2. Separate a change from its return value
What does this program display on its second line?
Select one choice, then check.
HintFollow both effects
values changes before the result of append is assigned to returned.
SolutionThe method returns None
The first line is [18, 21]. The second line is None because append
reports no replacement value.
Build Derived Labels in the Same Way
A result list can contain values calculated during the loop. Here each accepted reading becomes a short label:
This displays:
['18 °C', '21 °C', '20 °C', '23 °C']
The source readings list remains as it was. The loop builds a separate result
named labels. Chapter 6 will examine what happens when two names refer to the
same changeable object; this lesson needs only the visible mutation of one
named result list.
Text behaves differently. A string is ordered and supports indexing and slicing, but an individual character cannot be replaced:
Instead, string operations return new text. The next lesson will keep each intermediate result visible.
Q3. Repair a lost result list
The program should display [18, 22]. Repair the line that collects each
available reading without replacing the list name with None.
Editable Python
Ready to run.
HintLet the list name remain in place
Remove accepted = from the append line.
SolutionAppend without reassignment
An ordinary loop can build a result list one item at a time. append changes
that list and returns None; it does not produce a replacement list. Text
cannot be changed character by character, so the next lesson will transform
it through explicit returned values.