1
00:00:00,720 --> 00:00:08,070
Let's talk about the last one reduce and this is a bit advanced and it might be a little bit difficult

2
00:00:08,070 --> 00:00:13,950
to grasp at first but it's good to know that it exists and you can learn more about it by practicing

3
00:00:13,950 --> 00:00:17,100
it and reading some documentation on your own.

4
00:00:17,100 --> 00:00:22,790
Now reduce doesn't come as part of the Python built in function.

5
00:00:22,980 --> 00:00:27,020
In order for us to use reduce we have to do something like this.

6
00:00:27,420 --> 00:00:32,610
Now you've never seen this before and this is something we're going to get to in the modules section

7
00:00:32,610 --> 00:00:33,620
of the course.

8
00:00:33,720 --> 00:00:39,810
But essentially what's happening here is when we downloaded Python interpreter and the python package

9
00:00:40,410 --> 00:00:47,790
in our case this reptile already does it for us we can import something from these funky tools and funk

10
00:00:47,790 --> 00:00:58,190
tools are what we call a tool belt that we can use for functional tools that comes with the python installation.

11
00:00:58,320 --> 00:01:02,780
And from there there's a specific function that we can import.

12
00:01:02,820 --> 00:01:09,480
So I'm saying hey from funk tools from this tool belt I want you to import the reduce function so that

13
00:01:09,480 --> 00:01:13,950
we can use it in our code again something that we'll cover later on in the course.

14
00:01:13,950 --> 00:01:18,680
So for now just copy and paste this line next.

15
00:01:18,700 --> 00:01:26,920
Let's remove some of our list and here use reduce now with reduce.

16
00:01:26,920 --> 00:01:30,800
We're gonna need a few things first.

17
00:01:30,910 --> 00:01:37,510
We need the function and then we need the sequence or the data.

18
00:01:37,560 --> 00:01:40,030
So what's the function going to be.

19
00:01:40,230 --> 00:01:43,860
First of all our data will be our my list.

20
00:01:43,860 --> 00:01:52,070
So I'm going to say my list and the reduced function allows us to do something interesting.

21
00:01:52,200 --> 00:02:01,270
I can give it let's say a function and this function we'll call it the accumulator and the accumulator

22
00:02:01,270 --> 00:02:04,070
takes two parameters.

23
00:02:04,090 --> 00:02:06,490
And remember this is going to be called by reduce.

24
00:02:06,550 --> 00:02:12,430
So reduce is going to be in charge of giving these two parameters from the data that we give it which

25
00:02:12,430 --> 00:02:13,900
is my list.

26
00:02:14,140 --> 00:02:21,450
The first parameter is going to be what we call the accumulator or in my case the ICC.

27
00:02:21,730 --> 00:02:30,740
And then here is going to be the item now where it gets really interesting is that when we first pass

28
00:02:30,920 --> 00:02:41,690
my list from the reduce we'll get the first item the first item will be in this parameter however what's

29
00:02:41,840 --> 00:02:47,200
this HCC initially well if I just remove the brackets here

30
00:02:50,120 --> 00:02:58,690
and show you reduce you see that we have the function the sequence which in our case will be our list

31
00:02:59,440 --> 00:03:05,470
and then the initial an initial is what the accumulator is going to be.

32
00:03:06,100 --> 00:03:11,950
So once again let's say that we're gonna have the accumulator function that we're not going to call

33
00:03:11,950 --> 00:03:14,020
yet reduce is going to do that for us.

34
00:03:14,080 --> 00:03:21,380
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

35
00:03:21,470 --> 00:03:30,750
an initial value of 0 so we have 3 parameters here or three arguments that we give it.

36
00:03:30,830 --> 00:03:32,330
Let me close the brackets.

37
00:03:32,420 --> 00:03:41,770
Mary you make this a little bit bigger here and make sure that I can spell now in here the first item

38
00:03:41,860 --> 00:03:50,820
will be whatever is the first item in my list and then the accumulator is going to default to zero if

39
00:03:50,820 --> 00:03:51,950
we don't give it anything.

40
00:03:51,960 --> 00:03:56,440
Or in our case we have said that the accumulator is going to be zero.

41
00:03:56,520 --> 00:04:04,920
So the first thing we're gonna do is we're going to return accumulator plus item OK so let's go through

42
00:04:04,920 --> 00:04:08,910
this one by one I'm actually going to print here.

43
00:04:09,140 --> 00:04:13,490
The accumulator and the item just so we can see and then I'm going to run this code

44
00:04:16,470 --> 00:04:25,850
line 15 into object is not ignorable and this is a little tricky but we actually don't need the list

45
00:04:25,880 --> 00:04:28,920
anymore because reduce is gonna do something interesting for us.

46
00:04:29,000 --> 00:04:35,540
So let's just remove this for now and just return whatever reduce gives us and the print.

47
00:04:35,540 --> 00:04:39,580
If I click Run.

48
00:04:39,620 --> 00:04:39,860
All right.

49
00:04:39,890 --> 00:04:41,580
So what just happened.

50
00:04:41,630 --> 00:04:42,570
Let's have a look.

51
00:04:43,650 --> 00:04:52,050
Reduce allows us to reduce something some sort of value from the editable that we give it.

52
00:04:52,080 --> 00:04:53,450
So let's go step by step.

53
00:04:53,490 --> 00:05:02,970
We have my list and my list is going to be applied to accumulator through reduce the accumulator is

54
00:05:02,970 --> 00:05:09,690
going to take my list and the first thing it's going to do in the first pass through is going to say

55
00:05:09,690 --> 00:05:12,510
Hey what's my accumulator going to be.

56
00:05:12,510 --> 00:05:14,410
I'm going to set it to zero.

57
00:05:14,520 --> 00:05:18,110
So the first pass through accumulator is going to be zero.

58
00:05:18,240 --> 00:05:22,920
An item is going to be one which is what we see over here.

59
00:05:23,250 --> 00:05:27,290
And we're going to add it 0 plus 1.

60
00:05:27,310 --> 00:05:31,890
So now we have returned 1.

61
00:05:31,930 --> 00:05:35,460
Now the second pass to what's going to happen.

62
00:05:35,590 --> 00:05:44,820
The second pass through the accumulator is going to be what this returned so the next pass to whatever

63
00:05:44,850 --> 00:05:48,450
this is returned is going to be filled into accumulator.

64
00:05:48,450 --> 00:05:57,160
So in our case the 0 plus 1 will be 1 an item will be two one plus two is going to be three.

65
00:05:57,300 --> 00:05:59,910
So one plus two equals two three.

66
00:05:59,910 --> 00:06:03,730
So the next time around the accumulator is going to be three.

67
00:06:03,810 --> 00:06:11,460
And while we pass through is going to be three as well as you can see here and three plus three is six

68
00:06:12,180 --> 00:06:19,080
we've just accumulated all these values and returned one single number six.

69
00:06:19,080 --> 00:06:28,250
We've reduced our list into some sort of data that we've manipulated using in our case accumulator.

70
00:06:28,260 --> 00:06:38,240
But I could have done anything I wanted here so if for example I change this to not equal 10 what do

71
00:06:38,240 --> 00:06:48,030
you think will happen if I click Run I get 16 because I started my initial accumulator with 10 so it

72
00:06:48,030 --> 00:06:56,590
was 10 Plus 1 then so on and so forth now reduces really really powerful because you can do a lot of

73
00:06:56,590 --> 00:06:57,240
things with it.

74
00:06:57,250 --> 00:07:05,230
It's really really flexible and actually underneath the hood functions like a map filter are actually

75
00:07:05,230 --> 00:07:11,350
using this reduce underneath the hood so you can actually build your own map and filter function using

76
00:07:11,530 --> 00:07:18,790
reduce again reduce is a little hard to understand at first and it does take a bit of practice to really

77
00:07:18,820 --> 00:07:25,390
use it but it's one of those things that advanced programmers really really love and you'll see more

78
00:07:25,390 --> 00:07:29,470
and more of throughout your career as you get better and better.

79
00:07:29,470 --> 00:07:30,880
I'll see you in the next one.

80
00:07:30,900 --> 00:07:31,180
Bye bye.
