Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Compute running totals with accumulate

Use itertools.accumulate to compute cumulative sums efficiently.

Python practice16 minModules & Standard LibraryIntermediateLast updated March 23, 2026

Problem statement

Given an iterable of numeric values, return a list where each element is the running total (cumulative sum) up to that index. Use itertools.accumulate for an efficient implementation. The function should handle integers and floats and should return an empty list for an empty iterable.

Task

Write running_total that returns a list of cumulative sums from an iterable of numbers using itertools.accumulate.

Examples

Running totals of integers

Input

running_total([1, 2, 3, 4])

Output

[1, 3, 6, 10]

Cumulative sums: 1, 1+2=3, 3+3=6, 6+4=10

Input format

A list (or iterable) of numeric values: running_total(values)

Output format

A list of cumulative sums. Example: [v0, v0+v1, v0+v1+v2, ...]

Constraints

Use itertools.accumulate. Do not print; return a list. Should work with int and float and return [] for empty input.

Samples

Sample 1

Input

running_total([10, -2, 3])

Output

[10, 8, 11]

10, then 10+(-2)=8, then 8+3=11