Lambda expressions in Python

Rob Percival
A free video tutorial from Rob Percival
Web Developer And Teacher
4.5 instructor rating • 41 courses • 1,804,160 students

Lecture description

Learn about lambda expressions in Python in this video. Lambda expressions are very useful small—and sometimes anonymous—functions.

Learn more from the full course

The Complete Python & PostgreSQL Developer Course

Build 9 projects—master two essential and modern technologies in Python and PostgreSQL

21:39:30 of on-demand video • Updated April 2020

  • Understand software and programming with Python from beginning to end
  • Create Python applications ranging from beginner, basic applications to advanced, useful and engaging applications
  • Learn how to extend Python applications by using a professional data storage which is PostgreSQL
  • Understand advanced concepts extremely useful in jobs and interviews, such as data structures and algorithms
English I will come back in this really we're talking about lambda expressions lambda expressions is something that's gotten into a lot of programming languages and it's just like an anonymous function a function that doesn't have to have a name although it can do. And it's used normally to simplify doing things repetitively. I'll give you a couple of examples. In this video. Let's create a new file called lavenders and we're going to be creating a few of them here. The first lambda I wanted to create is just like a normal function. I'm going to call my lambda Eyl you can call it whatever you want and it's going to be equal to a lambda X and then colon X greater than 5. Now have you ever seen a lambda expression before in a different language I'm sure you know what this means. But really what this means is Aile is a lambda function that takes in a parameter called X and it returns whether x is greater than 5. So for all intents and purposes this is the same as def L X return Xclusive and 5 both on the same thing here. I'm calling the function L. It takes in the parameter anx and it returns whether x is greater than 5. That's my lambda function. And as a normal function I can print l 10 for example. And I'm printing passing 10 as the parameter x returning whether 10 is greater than 5. So we should see true printed out here and we do. So you know I'm not lying to you which is good and sometimes it useful to have these small and simple functions like functions like these but it's even better because it's really easy to pass as function arguments. Having these functions there by themselves can be useful but it's even more useful if we can use them in other places. So I'm going to create a function called term and it's going to take in a two parameters value and alteration . OK. And one is going to do is return the values inside this list which I'm going to call values in fact this is it value if they match these this check you would call it check if we want. OK so what is going to do is say return all the values in there. If they match this check OK. So what to do is Vau for vowel in values if check. Wow. So what this does is it says create a new list which has the values of all the elements in values. We know this from list comprehension but we're only going to put the value inside the new list if it matches or it succeeds in checking at the check function. So check here is not a value it is a function because as you can see we are passing to it the parameter vowel. OK so this looks a bit weird but we can do things like. For example we have my list which has the values 1 2 3 4 5 and we can do now is say and the list is alter my list where lambda x is x not equal to 5. And this looks a bit weird. I agree with you. But what's happening now is that we're passing in the little lambda function as our check parameter . And the my list as values. So what we're doing now is saying get a new list of each of the values in values which is the list. If the check function succeeds and returns true the only moment when it's not going to return true is in number 5 because five not equal to 5 is false. I would recommend Now take a moment pause the video make sure you've read write this in your editor you run it. Play around with it. Take a moment to take it in. Essentially now that we've done that let's print another list and run that. And as you can see we get the values of 1 2 3 4. So this has succeeded in altering my list when the elements in my list did not match this Lamda or other did match this lambda expression. The only element that this returned falls for was five because five not equal to five is false for not equal to 5. It's true because they are not equal. OK. Now we could create a function and sort a check not 5 with x then we could create a function called check not 5 and make it return whether X the parameter is not equal to 5. Essentially the same as on lambda function and pass that function here without the brackets and Malwa we're passing is a function name and then we're going to be executing it here. This is also fine and we can do that as well but it's just a bit more code essentially for no reason . We don't need to reuse the lambda function anywhere else so it doesn't make sense to define it and give it a name. Creating a lambda function is a lot cheaper in terms of performance than creating a full blown function keys. So we've got a couple of things in this video which is that we can pass what will lambda functions are . First of all we've also learned that we can pass them as parameters to functions. And as an addendum we've learned that we can also pass normal functions as parameters to methods which we didn't know before. And it's not something you maybe do very often but nevertheless you can do that. Just remember to not pass it with the brackets. If you pass this what you are passing when you put the brackets at the end of the function is the return value. So check not 5 5 would be false. And here what would be passing is false. And then you would try to run false with a parameter and you will get an error which is that false is not callable the Boolean object is not callable you can't call it with a set of brackets and a parameter because it is not a function. So when you want to pass a function as a parameter make sure to not include the brackets. So we've learned that we can do this stuff here. We can pass functions or parameters we've learned about lambda functions and by passing those as well . Now we are going to be learning a bit about some more useful scenarios for lambda functions but we're going to be doing that in the very next video because I don't want to bore you too much. So I'll see you in the very next one. And we're going to be looking at a bit more useful scenarios for lambda functions. So either