Course contentsShow
← Python
Lesson 687 of 1,06011. Standard Library EssentialsFree lesson

Accumulating Running Totals and State

Apply itertools.accumulate to perform rolling calculations or cumulative state updates across a stream of values without manual loop management.

Accumulating Running Totals and State

In this lesson, you will learn how to maintain running totals and state across a sequence of values efficiently using itertools.accumulate.

Beyond Manual Accumulation

When processing streams of data, you often need to transform a sequence into a "running" version of itself, such as calculating the balance of an account after each transaction or tracking the maximum value seen so far. Traditionally, you might use a for loop, a temporary variable to hold the state, and a list to store the results:

data = [10, 20, 5, 40]
running_total = []
current = 0
for value in data:
    current += value
    running_total.append(current)
# Result: [10, 30, 35, 75]

While this works, manual loop management increases code verbosity and the surface area for logic errors. The itertools.accumulate function replaces this pattern by yielding the accumulated state at every step of the iteration.

Using itertools.accumulate

The accumulate function takes an iterable and an optional binary function (a function that takes two arguments). By default, it performs addition, but you can pass any function to define the "state transition" logic.

To track the running maximum, for example, you can pass max:

from itertools import accumulate

prices = [10, 25, 15, 30, 20]
running_max = list(accumulate(prices, max))
# Result: [10, 25, 25, 30, 30]

Here, accumulate keeps track of the "state" (the highest price seen so far) and yields it every time a new element is processed. Because it returns an iterator, it is memory-efficient and avoids creating intermediate structures until you are ready to consume them.

This approach is highly effective for stateful transformations where the next value in your sequence depends on the accumulated result of the previous elements. Whether you are calculating financial balances, cumulative frequencies, or peak values, accumulate abstracts away the plumbing of state management, allowing you to focus on the logic of the transformation itself.

Key Takeaway: Use itertools.accumulate to transform a stream of data into a stream of stateful results without the need for manual loops or external tracking variables.