Lambda functions, also known as anonymous methods, are not new or unique to the Python language, but they sure are useful. In this lesson, I'll show you once again how to make your code more concise by combining this new concept of lambdas with other functions like
1.Introduction2 lessons, 04:55
2.Collections7 lessons, 46:55
3.File I/O6 lessons, 48:51
4.Conclusion1 lesson, 01:19
So it's time to be a little honest with ourselves here. And in a few of the last lessons, when we were talking about things like the map function or the filter function, we kinda were dealing with some functions that were maybe a little less than sweet. And one of the examples here that we were using was this sweet function that took in a value of x and it multiplied by two and then it squared that value and then return it back out. Now odds are this is not a very useful function. And if you start to think about the definition of a function, a function is really something or a group of functionality or a set of functionality that is reuseable that you're gonna reuse in different parts of your application that you might find use for in different areas. But, in all honesty, this function isn't very sweet. There's really not a whole lot of reusability here. Just because it's a very contrived example. So what's the point of creating a function and wasting space by defining this function that we can use in other places that odds are we're probably not gonna use anyway. So, what can we do about that? Well, so now is a good time to introduce the concept of a lambda. Now the concept of a lambda is not unique to Python. It's not a new thing, it's been around for a while. You may have heard of it as a lambda expression or maybe an anonymous function or things along those lines where basically the concept is you're able to define the equivalent of a function A without a name. So you don't have to store a name to be able to pass this function around. You're really just going to be able to use it in a single spot, and B, you can define it in line. Which means you don't have to declare it somewhere else like we're doing here. We're defining this function here suite, and it's somewhere else in a file that's living somewhere that you might have to import in your python file or something like. I can just define it in line. So let's revisit our example where we were using our map function, and let's do the same thing we're doing here, but let's get rid of this suite function because in all honesty, it's not very helpful. So how can we get rid of that but still have this line function in the same way or at least in a similar way? So now we're going to use a lambda. So the way that we do that is we specify the keyword lambda. Now once I've done that, that means that tells Python that I'm about to define an anonymous function that has inputs, that has outputs, that does what you would typically figure to be a defined function, but it's going to be done in line. So the way that I do that is I specify my input. So it would be X in this case, since our input here is X. Then we specify a colon. And then after that, we would specify what the body of the function would be. And in this case, with a lambda we're talking about a single line. So we're not talking about being able to take very complex multi-line functions and converting them into lambdas, because in that case it's not really going to work. We have to specify single line or one liners, if you will. So in this case, we want to return, and because it's a single line we don't have to actually use the return keyword. Our body of our lambda or of our function here is gonna be this right here. So let's go ahead and copy that and we'll put it down here. So this is our lambda, this whole thing right here is an anonymous function. It's a lambda that I can then put in line anywhere within my application that references suite and we should have the same exact functionality. So all I'm gonna do here is I'm going to copy this line just so you can see it side by side. And then I will take this lambda here and I will cut this and I will paste it in here where I reference my sweet function, and that's it. So as you can see, any place that takes in a function that's predefined somewhere else can also accept a lambda. So now if I save this, and I'll come back over to my command line, and I'll go ahead and run Python lambda. If I run that, you should see the exact same answer. I see the same values coming back, which means I can replace all of this with this one single line here. So I can delete all of this. Get this down to one individual line, I can save this. And I'll go ahead run it again and I'll get the same answer. So as you can see, using a powerful part of the Python language, the lambda expression or an anonymous function, I can now take what used to be a predefined function that maybe wasn't that reusable that I wasn't using around in a bunch of other places. And instead of wasting space on defining that somewhere when I'm not going to reuse it, I can define it in line using a lambda anywhere in any function that accepts another function. So it's a very cool way to condense your code and to keep things as in line as possible, and not take up a lot of space with things that you're just not gonna reuse.