Let's talk about the last one reduce and this is a bit advanced and it might be a little bit difficult
to grasp at first but it's good to know that it exists and you can learn more about it by practicing
it and reading some documentation on your own.
Now reduce doesn't come as part of the Python built in function.
In order for us to use reduce we have to do something like this.
Now you've never seen this before and this is something we're going to get to in the modules section
of the course.
But essentially what's happening here is when we downloaded Python interpreter and the python package
in our case this reptile already does it for us we can import something from these funky tools and funk
tools are what we call a tool belt that we can use for functional tools that comes with the python installation.
And from there there's a specific function that we can import.
So I'm saying hey from funk tools from this tool belt I want you to import the reduce function so that
we can use it in our code again something that we'll cover later on in the course.
So for now just copy and paste this line next.
Let's remove some of our list and here use reduce now with reduce.
We're gonna need a few things first.
We need the function and then we need the sequence or the data.
So what's the function going to be.
First of all our data will be our my list.
So I'm going to say my list and the reduced function allows us to do something interesting.
I can give it let's say a function and this function we'll call it the accumulator and the accumulator
takes two parameters.
And remember this is going to be called by reduce.
So reduce is going to be in charge of giving these two parameters from the data that we give it which
is my list.
The first parameter is going to be what we call the accumulator or in my case the ICC.
And then here is going to be the item now where it gets really interesting is that when we first pass
my list from the reduce we'll get the first item the first item will be in this parameter however what's
this HCC initially well if I just remove the brackets here
and show you reduce you see that we have the function the sequence which in our case will be our list
and then the initial an initial is what the accumulator is going to be.
So once again let's say that we're gonna have the accumulator function that we're not going to call
yet reduce is going to do that for us.
I'm going to give it the list or the data that we want to iterate over and then I'm going to give it
an initial value of 0 so we have 3 parameters here or three arguments that we give it.
Let me close the brackets.
Mary you make this a little bit bigger here and make sure that I can spell now in here the first item
will be whatever is the first item in my list and then the accumulator is going to default to zero if
we don't give it anything.
Or in our case we have said that the accumulator is going to be zero.
So the first thing we're gonna do is we're going to return accumulator plus item OK so let's go through
this one by one I'm actually going to print here.
The accumulator and the item just so we can see and then I'm going to run this code
line 15 into object is not ignorable and this is a little tricky but we actually don't need the list
anymore because reduce is gonna do something interesting for us.
So let's just remove this for now and just return whatever reduce gives us and the print.
If I click Run.
All right.
So what just happened.
Let's have a look.
Reduce allows us to reduce something some sort of value from the editable that we give it.
So let's go step by step.
We have my list and my list is going to be applied to accumulator through reduce the accumulator is
going to take my list and the first thing it's going to do in the first pass through is going to say
Hey what's my accumulator going to be.
I'm going to set it to zero.
So the first pass through accumulator is going to be zero.
An item is going to be one which is what we see over here.
And we're going to add it 0 plus 1.
So now we have returned 1.
Now the second pass to what's going to happen.
The second pass through the accumulator is going to be what this returned so the next pass to whatever
this is returned is going to be filled into accumulator.
So in our case the 0 plus 1 will be 1 an item will be two one plus two is going to be three.
So one plus two equals two three.
So the next time around the accumulator is going to be three.
And while we pass through is going to be three as well as you can see here and three plus three is six
we've just accumulated all these values and returned one single number six.
We've reduced our list into some sort of data that we've manipulated using in our case accumulator.
But I could have done anything I wanted here so if for example I change this to not equal 10 what do
you think will happen if I click Run I get 16 because I started my initial accumulator with 10 so it
was 10 Plus 1 then so on and so forth now reduces really really powerful because you can do a lot of
things with it.
It's really really flexible and actually underneath the hood functions like a map filter are actually
using this reduce underneath the hood so you can actually build your own map and filter function using
reduce again reduce is a little hard to understand at first and it does take a bit of practice to really
use it but it's one of those things that advanced programmers really really love and you'll see more
and more of throughout your career as you get better and better.
I'll see you in the next one.
Bye bye.