WEBVTT

1
00:00:00.670 --> 00:00:02.250
<v Instructor>Hi guys, and welcome back.</v>

2
00:00:02.250 --> 00:00:05.094
In this video, we're going to learn about lambda functions.

3
00:00:05.094 --> 00:00:08.680
A lambda function is a different type of function

4
00:00:08.680 --> 00:00:10.368
which doesn't have a name,

5
00:00:10.368 --> 00:00:14.038
and is only used to return values.

6
00:00:14.038 --> 00:00:16.490
Remember that we mentioned that functions can be used

7
00:00:16.490 --> 00:00:19.231
to perform actions such as printing something out,

8
00:00:19.231 --> 00:00:22.540
or operating with values and returning them,

9
00:00:22.540 --> 00:00:26.030
such as the sum function, or they can do both.

10
00:00:26.030 --> 00:00:28.600
But lambda functions are exclusively used

11
00:00:28.600 --> 00:00:31.150
to operate on inputs and return outputs.

12
00:00:31.150 --> 00:00:34.253
They are almost never used to perform actions.

13
00:00:35.340 --> 00:00:38.550
Let's start with a simple function that gets some input

14
00:00:38.550 --> 00:00:40.110
and returns some output.

15
00:00:40.110 --> 00:00:42.940
Let's go to our trusty add function

16
00:00:42.940 --> 00:00:45.764
that returns x + y.

17
00:00:45.764 --> 00:00:49.260
If you print add of five and seven,

18
00:00:49.260 --> 00:00:52.110
you've got that add five and seven

19
00:00:52.110 --> 00:00:53.690
is going to run this line here,

20
00:00:53.690 --> 00:00:56.240
add x and y together, which gives you 12,

21
00:00:56.240 --> 00:00:57.410
and then it's gonna return it,

22
00:00:57.410 --> 00:01:00.470
replacing essentially this entire function call by

23
00:01:00.470 --> 00:01:03.150
the value 12, and then we're going to print that out.

24
00:01:03.150 --> 00:01:06.043
So, if we press play on this, you get 12.

25
00:01:07.350 --> 00:01:10.860
You can rewrite this function as a lambda function,

26
00:01:10.860 --> 00:01:12.480
which would be like this.

27
00:01:12.480 --> 00:01:17.480
Lambda x, y: x + y.

28
00:01:17.550 --> 00:01:19.500
So what we've got here,

29
00:01:19.500 --> 00:01:21.050
and ignore this underline for a second,

30
00:01:21.050 --> 00:01:23.670
what we've got here is the lambda keyword,

31
00:01:23.670 --> 00:01:25.930
then we've got a space, and then we've got

32
00:01:25.930 --> 00:01:30.410
the list of parameters, which in this case are x and y,

33
00:01:30.410 --> 00:01:32.730
then we've got a colon, and then we've got

34
00:01:32.730 --> 00:01:34.610
the return value of the function,

35
00:01:34.610 --> 00:01:37.260
but we don't specify the return keyword.

36
00:01:37.260 --> 00:01:39.590
So lambda functions are used to return output,

37
00:01:39.590 --> 00:01:42.050
so you don't need to specify return because

38
00:01:42.050 --> 00:01:44.872
that's what they're for, so Python will infer that.

39
00:01:44.872 --> 00:01:47.780
So, this does exactly the same thing as the add function

40
00:01:47.780 --> 00:01:49.710
earlier, but it doesn't have a name.

41
00:01:49.710 --> 00:01:52.910
Notice that the add variable is never created.

42
00:01:52.910 --> 00:01:57.060
So to solve that problem, if you want your lambda functions

43
00:01:57.060 --> 00:01:58.820
to have a name, which sometimes you won't

44
00:01:58.820 --> 00:02:00.550
and I'll show you why not in a moment,

45
00:02:00.550 --> 00:02:03.300
you can just assign that to a variable.

46
00:02:03.300 --> 00:02:05.010
So here we've got the add variable,

47
00:02:05.010 --> 00:02:07.150
make it equal to our lambda function,

48
00:02:07.150 --> 00:02:09.630
and then you can call it in your programme using add.

49
00:02:09.630 --> 00:02:11.600
So again, there are four parts to a lambda function.

50
00:02:11.600 --> 00:02:13.130
You've got the lambda keyword,

51
00:02:13.130 --> 00:02:14.730
then you've got your parameter list,

52
00:02:14.730 --> 00:02:16.060
then you've got your colon,

53
00:02:16.060 --> 00:02:20.530
and finally, your return value without the return keyword.

54
00:02:20.530 --> 00:02:22.950
Lambdas are meant to be short functions,

55
00:02:22.950 --> 00:02:25.830
often used without giving them a name,

56
00:02:25.830 --> 00:02:27.210
for example in conjunction with the

57
00:02:27.210 --> 00:02:29.110
built in function called map.

58
00:02:29.110 --> 00:02:31.568
So we're going to learn about map in a moment.

59
00:02:31.568 --> 00:02:34.960
However, you can give them a name if you want.

60
00:02:34.960 --> 00:02:38.750
If you didn't want to give it a name, then you would have

61
00:02:38.750 --> 00:02:42.500
to use it in the same line where you define it.

62
00:02:42.500 --> 00:02:44.400
Otherwise as soon as you define this function,

63
00:02:44.400 --> 00:02:46.070
you have no way of actually using it,

64
00:02:46.070 --> 00:02:47.310
because it doesn't have a name, so it's

65
00:02:47.310 --> 00:02:48.920
kind of pointless to just define

66
00:02:48.920 --> 00:02:50.915
the lambda function without using it.

67
00:02:50.915 --> 00:02:53.868
If you wanted to use this lambda function,

68
00:02:53.868 --> 00:02:57.180
you need to surround it in brackets

69
00:02:57.180 --> 00:02:59.731
so that Python will treat this as single unit,

70
00:02:59.731 --> 00:03:02.810
and then call the function here, say with

71
00:03:02.810 --> 00:03:04.850
the values five and seven,

72
00:03:04.850 --> 00:03:07.429
and this here means that you are using

73
00:03:07.429 --> 00:03:11.060
the five and seven call on this thing,

74
00:03:11.060 --> 00:03:13.270
which is a lambda function, which essentially

75
00:03:13.270 --> 00:03:16.210
calls the function with the arguments five and seven.

76
00:03:16.210 --> 00:03:19.020
You can see why this is also not very often done,

77
00:03:19.020 --> 00:03:22.240
because it's extremely confusing, and this will still

78
00:03:22.240 --> 00:03:23.900
not do anything, 'cause you still need

79
00:03:23.900 --> 00:03:26.290
your print function around it as well.

80
00:03:26.290 --> 00:03:28.240
So lambda functions used in this way,

81
00:03:28.240 --> 00:03:30.030
not very common at all.

82
00:03:30.030 --> 00:03:32.330
Usually you will define a proper function

83
00:03:32.330 --> 00:03:34.860
and use it like we did earlier on instead.

84
00:03:34.860 --> 00:03:37.880
But let's get into some uses for lambda functions

85
00:03:37.880 --> 00:03:39.305
that are actually useful.

86
00:03:39.305 --> 00:03:42.260
Let's say you've got a function called double

87
00:03:42.260 --> 00:03:44.280
that doubles a number passed into it,

88
00:03:44.280 --> 00:03:46.700
and then you've got a sequence of numbers,

89
00:03:46.700 --> 00:03:49.040
such as one, three, five, and nine.

90
00:03:49.040 --> 00:03:51.870
And you want to double this sequence, or you want

91
00:03:51.870 --> 00:03:54.320
to double each number in this sequence.

92
00:03:54.320 --> 00:03:57.110
Something that you may see is something like doubled equal,

93
00:03:57.110 --> 00:04:02.110
and then use a list comprehension, x * 2 for x in sequence.

94
00:04:02.955 --> 00:04:07.910
So x * 2 is the same as this function returns

95
00:04:07.910 --> 00:04:11.160
for its given input, so instead we can do

96
00:04:11.160 --> 00:04:13.900
double x for x in sequence.

97
00:04:13.900 --> 00:04:17.500
The benefit of this is that using a function call

98
00:04:17.500 --> 00:04:20.210
allows you to potentially perform larger

99
00:04:20.210 --> 00:04:22.310
or more complicated mathematical expressions

100
00:04:22.310 --> 00:04:25.320
without making your list comprehension more complicated.

101
00:04:25.320 --> 00:04:29.150
This specific point in time it's not a big difference

102
00:04:29.150 --> 00:04:31.934
but it can be in other scenarios.

103
00:04:31.934 --> 00:04:34.980
So instead of doing this list comprehension,

104
00:04:34.980 --> 00:04:36.350
you can use the map function.

105
00:04:36.350 --> 00:04:41.350
And that is where lambda functions become extremely useful.

106
00:04:41.890 --> 00:04:43.730
So instead of using that, you can do

107
00:04:43.730 --> 00:04:45.330
doubled = map(double, sequence).

108
00:04:48.020 --> 00:04:51.140
And that does exactly the same as this list comprehension.

109
00:04:51.140 --> 00:04:54.960
What map does is it goes over each value in the sequence

110
00:04:54.960 --> 00:04:57.820
such as a list, a double, a set, and so on,

111
00:04:57.820 --> 00:05:01.350
and it applies the double function on each element,

112
00:05:01.350 --> 00:05:04.210
and then returns the final sequence out to you.

113
00:05:04.210 --> 00:05:05.360
So that's what map is for,

114
00:05:05.360 --> 00:05:07.770
it's very useful, so it is the counterpart

115
00:05:07.770 --> 00:05:10.240
for this list comprehension here.

116
00:05:10.240 --> 00:05:13.443
In Python, you would usually use list comprehensions,

117
00:05:13.443 --> 00:05:16.780
but many other programming languages don't have

118
00:05:16.780 --> 00:05:19.956
list comprehensions, so they have to use things like map.

119
00:05:19.956 --> 00:05:23.980
So, if you are working with developers familiar

120
00:05:23.980 --> 00:05:26.150
with other programming languages, and you want

121
00:05:26.150 --> 00:05:30.060
to use a programming style that they will be familiar with,

122
00:05:30.060 --> 00:05:31.570
you can use something like map.

123
00:05:31.570 --> 00:05:33.890
In addition, if you're working in a large project

124
00:05:33.890 --> 00:05:36.530
and you are using multiple different programming languages,

125
00:05:36.530 --> 00:05:39.368
such as JavaScript as well as Python,

126
00:05:39.368 --> 00:05:42.730
you may want to use map so that your Python code

127
00:05:42.730 --> 00:05:46.090
and your JavaScript code both have similar style.

128
00:05:46.090 --> 00:05:49.020
With that said though, the list comprehension

129
00:05:49.020 --> 00:05:53.300
is usually slightly faster, so there is

130
00:05:53.300 --> 00:05:56.190
no reason to use things like map

131
00:05:56.190 --> 00:06:00.373
over list comprehensions most of the time in Python.

132
00:06:00.373 --> 00:06:03.897
Let's re-write this example using a lambda function.

133
00:06:03.897 --> 00:06:08.440
Your doubled list comprehension here could define

134
00:06:08.440 --> 00:06:12.244
a lambda function here, lambda x: x * 2,

135
00:06:12.244 --> 00:06:16.636
and then use x as the argument to that function,

136
00:06:16.636 --> 00:06:19.300
and then you no longer need this doubled there,

137
00:06:19.300 --> 00:06:22.020
but this is getting quite confusing,

138
00:06:22.020 --> 00:06:24.330
because as I mentioned earlier on, you have your

139
00:06:24.330 --> 00:06:26.880
lambda function, which must be surrounded by brackets,

140
00:06:26.880 --> 00:06:29.070
and then you have another pair of brackets

141
00:06:29.070 --> 00:06:32.260
to pass in the argument, so this is a little bit unreadable.

142
00:06:32.260 --> 00:06:34.320
So if you want to use lambda functions

143
00:06:34.320 --> 00:06:38.320
you should use map instead, just because

144
00:06:38.320 --> 00:06:41.360
it makes it a little bit clearer without so many brackets

145
00:06:41.360 --> 00:06:42.445
what's going on.

146
00:06:42.445 --> 00:06:45.020
By the way, if you want to run this code

147
00:06:45.020 --> 00:06:47.350
and check the value returned by map,

148
00:06:47.350 --> 00:06:50.260
you need to turn it into a list in order to be able

149
00:06:50.260 --> 00:06:52.460
to print it out, just because the map function

150
00:06:52.460 --> 00:06:54.760
doesn't return a list of numbers,

151
00:06:54.760 --> 00:06:57.520
it will return something called a map object.

152
00:06:57.520 --> 00:06:59.430
We're going to learn more about what those things are

153
00:06:59.430 --> 00:07:01.020
as we move through these videos,

154
00:07:01.020 --> 00:07:02.630
but for now just turn it into a list

155
00:07:02.630 --> 00:07:05.470
if you want to see the values in your console.

156
00:07:05.470 --> 00:07:09.240
So just a recap, lambdas are functions without a name.

157
00:07:09.240 --> 00:07:11.498
You can give them a name by assigning them to a variable,

158
00:07:11.498 --> 00:07:15.010
but they don't have a name in the function definition.

159
00:07:15.010 --> 00:07:17.530
They are used to return a value calculated

160
00:07:17.530 --> 00:07:19.280
from its parameters, and they're almost

161
00:07:19.280 --> 00:07:21.366
never used to perform actions.

162
00:07:21.366 --> 00:07:24.810
They're almost always single line, so you don't want

163
00:07:24.810 --> 00:07:27.120
to be doing anything really complicated in them,

164
00:07:27.120 --> 00:07:29.260
and one of the big reasons why they're single line

165
00:07:29.260 --> 00:07:32.570
is because their syntax gets really complicated

166
00:07:32.570 --> 00:07:34.859
to read as they get longer.

167
00:07:34.859 --> 00:07:38.570
Very often it's better to define a normal function

168
00:07:38.570 --> 00:07:41.130
than a lambda function, in most situations.

169
00:07:41.130 --> 00:07:43.870
Lambda functions, I personally don't use them very much,

170
00:07:43.870 --> 00:07:46.100
because I find them that they are a bit difficult to read

171
00:07:46.100 --> 00:07:48.926
and keep track of, especially as your programmes grow.

172
00:07:48.926 --> 00:07:51.460
And that's it for this video, thanks for joining me,

173
00:07:51.460 --> 00:07:53.053
and I'll see you in the next one.

