9.12 The Accumulator Pattern with Lists
From Foundations of Python ProgrammingGNU FDL 1.3Original pagePython1 class period4 exercises4 graded tests
9.12 The Accumulator Pattern with Lists
Read first in the book: 9.1 Introduction: Transforming Sequences, 9.2 Mutability, 9.3 List Element Deletion, 9.4 Objects and References, 9.5 Aliasing, 9.6 Cloning Lists, 9.7 Mutating Methods, 9.8 Append versus Concatenate, 9.9 Non-mutating Methods on Strings, 9.10 String Format Method, 9.11 f-Strings.
We can accumulate values into a list rather than accumulating a single numeric value. Consider, for example, the following program which transforms a list into a new list by squaring each of the values.
nums = [3, 5, 8]
accum = []
for w in nums:
x = w**2
accum.append(x)
print(accum)
Here, we initialize the accumulator variable to be an empty list, on line 2.
We iterate through the sequence (line 3). On each iteration we transform the item by squaring it (line 4).
The update step appends the new item to the list which is stored in the accumulator variable
(line 5). The update happens using the .append(), which mutates the list rather than using a
reassignment. Instead, we could have written accum = accum + [x], or accum += [x]. In either
case, we'd need to concatenate a list containing x, not just x itself.
At the end, we have accumulated a new list of the same length as the original, but with each item transformed into a new item. This is called a mapping operation, and we will revisit it in a later chapter.
Note how this differs from mutating the original list, as you saw in a previous section.
Check your understanding
Check your understanding: a multiple-choice question in the book.
Check your understanding: a multiple-choice question in the book.
Exercise 1
- For each word in the list
verbs, add an -ing ending. Save this new list in a new list,ing.
Open the
01folder and editmain.py. Run Tests checks it.
Exercise 2
Given the list of numbers, numbs, create a new list of those same numbers increased by 5. Save this new list to the variable newlist.
Open the
02folder and editmain.py. Run Tests checks it.
Exercise 3
Given the list of numbers, numbs, modifiy the list numbs so that each of the original numbers are increased by 5. Note this is not an accumulator pattern problem, but its a good review.
Open the
03folder and editmain.py. Run Tests checks it.
Exercise 4
For each number in lst_nums, multiply that number by 2 and append it to a new list called larger_nums.
Open the
04folder and editmain.py. Run Tests checks it.
For teachers
9.12 The Accumulator Pattern with Lists
Students read Foundations of Python Programming's “The Accumulator Pattern with Lists” in CS Room and work through its 4 Python coding exercises (4 graded).
- Time: 1 class period
- Tests: 4 in 4 graded exercises, set up for you when you import the lesson
Reading first
- 9.1 Introduction: Transforming Sequences
- 9.2 Mutability
- 9.3 List Element Deletion
- 9.4 Objects and References
- 9.5 Aliasing
- 9.6 Cloning Lists
- 9.7 Mutating Methods
- 9.8 Append versus Concatenate
- 9.9 Non-mutating Methods on Strings
- 9.10 String Format Method
- 9.11 f-Strings
Exercises
| Folder | Grading | Tests |
|---|---|---|
01 | Autograded | 1 |
02 | Autograded | 1 |
03 | Autograded | 1 |
04 | Autograded | 1 |