How to Use the Python Reduce() function to Reduce a List into a Single Value

Created with Sketch.

How to Use the Python Reduce() function to Reduce a List into a Single Value

Summary: in this tutorial, you’ll learn how to use the Python reduce() function to process a list.

Reducing a list

Sometimes, you want to reduce a list to a single value. For example, suppose that you have a list of numbers:

scores = [75, 65, 80, 95, 50]

Code language: Python (python)

And to calculate the sum of all elements in the scores list, you can use a for loop like this:

scores = [75, 65, 80, 95, 50]

total = 0

for score in scores:
total += score

print(total)

Code language: Python (python)

Output:

365

Code language: Python (python)

In this example, we have reduced the whole list into a single value, which is the sum of all elements from the list.

Introduction the Python reduce() function

Python offers a function called reduce() that allows you to reduce a list in a more concise way.

Here is the syntax of the reduce() function:

reduce(fn,list)

Code language: Python (python)

The reduce() function applies the fn function of two arguments cumulatively to the items of the list, from left to right, to reduce the list into a single value.

Unlike the map() and filter() functions, the reduce() isn’t a built-in function in Python. In fact, the reduce() function belongs to the functools module.

To use the reduce() function, you need to import it from the functools module using the following statement at the top of the file:

from functools import reduce

Code language: Python (python)

Note that you’ll learn more about modules and how to use them in the later tutorial.

The following illustrates how to use the reduce() function to calculate the sum of elements of the scores list:

from functools import reduce

def sum(a, b):
print(f"a={a}, b={b}, {a} + {b} ={a+b}")
return a + b

scores = [75, 65, 80, 95, 50]
total = reduce(sum, scores)
print(total)

Code language: Python (python)

Output:

a=75, b=65, 75 + 65 = 140
a=140, b=80, 140 + 80 = 220
a=220, b=95, 220 + 95 = 315
a=315, b=50, 315 + 50 = 365
365

Code language: Python (python)

As you can see clearly from the output, the reduce() function cumulatively adds two elements of the list from left to right and reduces the whole list into a single value.

To make the code more concise, you can use a lambda expression instead of defining the sum() function:

from functools import reduce

scores = [75, 65, 80, 95, 50]

total = reduce(lambda a, b: a + b, scores)

print(total)

Code language: Python (python)

Summary

  • Use the Python reduce() function to reduce a list into a single value.

Leave a Reply

Your email address will not be published. Required fields are marked *