 # What Is the reduce() Function?

In this lesson, you’ll learn how to use the `reduce()` function (or `functools.reduce()` in Python 3), which is one of the functional programming primitives (or building blocks) available in Python. It takes a function, a sequence (or iterable), and an optional initial value.

You’ll walk through how you can use the `reduce()` function with the data set that you’ve been using in this course to reduce a sequence to a sinlge output value by applying a function repeatedly in order to output a single value. breaves

So you’ve basically defined the function def f(acc,val): return acc+val[‘age’]

So each item of the sequence is always applied to the SECOND argument of the function, right? Could as well be def f(x,y): return x+y[‘age’] and it’s basically calling for s in sequence: x=f(x,s) return x

right? Roy Telles

If I’m not mistaken, `reduce()` can be viewed as doing the `for` loop iterations for us:

We can expand the `reduce()` function to be something like:

``````def total_age(val, acc=0):
for item in val:
acc += item['age']

return acc
``````
• `acc=0` is what `reduce()` does when we pass the optional initial value `0` to it
• `acc += item['age']` is what `reduce()` does for us, at its core
• `return acc` is our ultimate value at the end of our iterations

to join the conversation.