Using the Python ‘reduce()’ Function to Aggregate Data, With Examples

Using Python reduce()

This article will show you how to use the Python reduce() function to aggregate data from values in an iterable (iterables are things like lists, dictionaries, sets, or tuples – collections containing items that can be looped over) and reduce them to a single accumulated value.

The code and examples in this tutorial will work in Python 3.

Syntax for Python reduce()

functools.reduce(FUNCTION, ITERABLE[, INITIALIZER])

Note that:

  • FUNCTION is a standard Python function, previously defined in your code
    • It should accept two parameters
      • The first parameter is the current accumulated value up until this point while processing items in the lists
      • The second parameter is used to read each item in the iterable passed to the filter() function
    • The return value of this function should be the will be passed to the next iteration as the first parameter
      • This is the new accumulated value, passed to the next iteration so that the next item in the list can be added to it (or subtracted from it, or whatever it is you want to do)
      • The FUNCTION isn’t limited to addition and subtraction – it’s just the most common usage
  • ITERABLE should be a variable or value containing an iterable
    • Lists, dictionaries, sets, and tuples are common iterables
  • INITIALIZER is the initial accumulated value before any items have been processed or the default value of the return value
    • It is optional
  • reduce() will return a single value – the accumulated value as it is after all items in ITERABLE have been processed
    • The type returned by reduce() will depend on what INITIALIZER and each item in the ITERABLE are
      • The FUNCTION used could add numbers, or it could join strings, for example

Example Usage of reduce()

This example uses reduce() to sum a list of temperatures so that they can be averaged:

# reduce() is part of the functools library, which must be imported
import functools

# Define a list of temperatures 
temperatures = [23, 5, -5, 19, -6, 11, -3]

# Define the function we wish to run for each item in the above list to reduce it to a single value
# This function takes two parameters - the first is the accumulated value for all items already processed, and the second is the current item from the list
def sumFunction(accumulated, item):
    # Adds the value of the current item from the list to the accumulated total
    return accumulated + item

# Use the reduce() function to run sumFunction on every item in the temperatures list
# Result will be the single accumulated value once all items in the temperature array have been processed by sumFunction
result = functools.reduce(sumFunction, temperatures)

# Calculate and print the average by dividing by the number of temperatures supplied (using len() to get the length of the temperatures array)
average = result / len(temperatures)
print(average)

Similar Python Functions

Several other Python Functions work similarly to map() but may suit your scenario better – give them a look:

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

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