‫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 download it Python interpreter and the Python package,
‫in our case, this ripple 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 going to 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 will 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 ACC.
‫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 Assisi 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 and initial is what the accumulator is going to be.
‫So once again, let's say that we're going to 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 zero.
‫So we have three.
‫Parameters here or three arguments that we give it.
‫Let me close the brackets.
‫There you go.
‫Let's 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 going to do is we're going to return accumulator plus item.
‫Okay.
‫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 iterable.
‫And this is a little tricky, but we actually don't need the list anymore because Reduce is going to
‫do something interesting for us.
‫So let's just remove this for now and just return whatever reduce gives us in the print.
‫If I click Run.
‫Look at that.
‫All right, so what just happened?
‫Let's have a look.
‫Reduce.
‫Allows us to reduce something, some sort of value from the iterable 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 zero plus one.
‫So now we have returned one.
‫Now the second path 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, zero plus one will be one.
‫An item will be.
‫One plus two is going to be three.
‫So one plus two equals to three.
‫So the next time around, the accumulator is going to be three and what 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 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 ten, what do you think will happen if I click Run?
‫I get 16 because I started my initial accumulator with ten, so it was ten plus one 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 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.