Editor

Compute a Running Total

Beginner · Write the code · from Python itertools: Powerful Tools for Working with Iterables

From the lesson: Python itertools: Powerful Tools for Working with Iterables

Imagine you have a Swiss Army knife for loops. Instead of writing complicated nested loops and temporary lists, you pull out the right tiny tool and the job is done in one clean line.

Read the lesson

Use accumulate to compute the running total of [10, 20, 30, 40, 50]. Convert to a list and print it.

from itertools import accumulate
values = [10, 20, 30, 40, 50]
# Compute running total and print

More itertools exercises

All Python exercises · 50 practice problems · Standard Library Essentials quiz