The Accumulator Pattern with ListsΒΆ

We can also 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.

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 of this chapter.

Check your understanding

Next Section - The Accumulator Pattern with Strings