Calculate sum using a list comprehension in Python

avatar

Borislav Hadzhiev

Last updated: Jul 4, 2022

banner

Photo from Unsplash

Calculate sum using a list comprehension in Python #

List comprehensions return a list, not a number, but we can calculate the sum of elements in a list in a similar way using the reduce() function. The reduce() function can be used to call a reducer function on each element and produce a single value.

main.py
from functools import reduce list_of_numbers = [5, 10, 15] total = reduce(lambda acc, current: acc + current, list_of_numbers) print(total) # 👉️ 30

We used the reduce() function to sum a list.

The reduce function takes the following 3 parameters:

NameDescription
functionA function that takes 2 parameters - the accumulated value and a value from the iterable.
iterableEach element in the iterable will get passed as an argument to the function.
initializerAn optional initializer value that is placed before the items of the iterable in the calculation.
The lambda function in the example takes the accumulated value and the current value as parameters and returns the sum of the two.

The final result of running the reducer function across the list items is a single value - the sum.

If we provide a value for the initializer argument, it is placed before the items of the iterable in the calculation.

main.py
from functools import reduce list_of_numbers = [5, 10, 15] total = reduce( # 👇️ accumulator is `0` on first iteration lambda acc, current: acc + current, list_of_numbers, 0 # 👈️ initial value of 0 ) print(total) # 👉️ 30

You can also extract the function and use a couple of print() calls to better understand how reduce works.

main.py
from functools import reduce list_of_numbers = [5, 10, 15] def get_sum(acc, current): print('accumulator: ', acc) print('current: ', current) return acc + current total = reduce( get_sum, list_of_numbers, 0 # 👈️ initial value of 0 (acc is 0 on first iteration) ) print(total) # 👉️ 30

In the example, we passed 0 for the initializer argument, so the value of the accumulator will be 0 on the first iteration.

The value of the accumulator would get set to the first element in the iterable if we didn't pass a value for the initializer.

main.py
from functools import reduce list_of_numbers = [5, 10, 15] def get_sum(acc, current): # 👇️ acc is 5 on first iteration print('accumulator: ', acc) print('current: ', current) return acc + current total = reduce( get_sum, list_of_numbers, ) print(total) # 👉️ 30

If the iterable is empty and the initializer is provided, the initializer is returned.

main.py
from functools import reduce list_of_numbers = [] def get_sum(acc, current): print('accumulator: ', acc) print('current: ', current) return acc + current total = reduce( get_sum, list_of_numbers, 0, # 👈️ set initializer to 0 ) print(total) # 👉️ 0

The list in the example above is empty, so the value of the initializer (0) is returned.

If the initializer is not provided and the iterable contains only 1 item, the first item is returned.

main.py
from functools import reduce list_of_numbers = [5] def get_sum(acc, current): print('accumulator: ', acc) print('current: ', current) return acc + current total = reduce( get_sum, list_of_numbers, ) print(total) # 👉️ 5

The list in the example only contains a single element and we didn't provide a value for the initializer, so the reduce() function returned the list element.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.