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 lessonUse 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
- Flatten a Nested ListBeginner
- Generate All Coin Flip OutcomesIntermediate
- Find All 2-Letter PermutationsIntermediate
- Predict the takewhile OutputIntermediate
- Group Numbers as Even or OddAdvanced
- Fix the groupby BugAdvanced
All Python exercises · 50 practice problems · Standard Library Essentials quiz