[MUSIC] In this video, I'm going to introduce you to the concept of anonymous functions. What is that? Well, as the name implies, they're functions that you write that have no name. Normally, when we write a function, we say def and then the name of the function, and then we continue from there. And now that function has a name and we can call it from anywhere by using that name. An anonymous function, well we just write it inline in our code somewhere where we need it and that's the only place it is. And you can't refer to it ever again, because it has no name, right? Now these are often called lambdas, and in Python you use the keyword lambda to create them. So let's take a look at what's going on here. Now, you usually use lambda's when you're creating a function that you're just going to pass to another function, and that's all you're going to do with it. So first I want to introduce to you to a function called map, which is a function that takes other functions as an argument. So first let's create a list, and I'm going to use my shortcut here of range, all right, and that gives me a list of numbers. Then I'm going to define a function called square, and all it does is take a number as an input, and return the square of that number. Now I want to take my list of data and my square function, and I want to apply the square function to every element in the list and create a new list. So I basically want a list of squares. Well, I'm sure you could do that with a loop, but the function map allows us to do this is in an easier way. Map takes at least two arguments. The first argument always needs to be a function, so I'm going to pass it square and then it can take sequences. So I'll pass it data. And what's going to happen here is it's going to call square on every element of data and create a new list with the results of all these calls. So let's run this and see what happens. Well, it did what you might expect. Or rather, it did exactly [LAUGH] what I said it was going to do. It created a list of all the squares of the elements in the input list. All right, well, where do the anonymous functions come in? Well, you might notice here that I had to define that square function simply to pass it to map. Well, why bother? Lambda's make that a little bit simpler. So let's try and do exactly the same thing, except this time I want to double all the numbers in the list. So instead of defining yet another function simply so I can pass it to map, let's use a lambda. Now, here is the syntax of a lambda. The keyword lambda is first, then the arguments to the function followed by a colon, and then you are allowed to have a single expression. So these are limited functions. They can't have multiple lines of code. You effectively can have one Python expression. So num * 2 here is the expression that I want, and I don't need a return statement. It basically evaluates the expression and that's what gets returned. So I pass to map. The first argument is this lambda, which is a function that takes a single argument and returns twice the input argument, and then my data list. So hopefully you know what's going to happen here. It's going to create a list of doubles or a list of even numbers, 0, 2, 4, 6, and so on. So hopefully you can see that this was somewhat valuable, because I can just create the lambda exactly where I need it. I do not have to define a function with all the associated overhead of doing so, just so that I can pass my function to a single other function. But you are limited in what you can do. You can take more than one argument. You can take as many arguments as you want, but you can only evaluate one single expression. All right, let's explore this a little bit further. I want to create another list and let's make this list a bunch of random numbers, okay? I'm going to use what's called a list comprehension here. And hopefully it is readily obvious what's happening. I used the square brackets to tell Python that this is a list, and then I'm using random.randrange. Wait a minute, what's that, [LAUGH] all right? Well, that's another function from the random module. Randrange says pick a random integer in the range from the first number up to, but not including, the second number. So from 2 to num+3, what's num+3? Well, I have for num in range (10). So what's going to happen is range (10) creates all the numbers from 0 to 9, assigns them each in turn to num, and then evaluates random.randrange with that num and creates a new list. So I'm going to have a 10 element list with 10 random numbers, and each number's going to be between 2, and the input +3. All right, let's see what we get out of this. I got a bunch of random numbers. That makes sense, it did exactly what I said it would. If you're interested in list comprehensions, I encourage you to read the Python documentation. Now what I want to do is create a list of tuples using the map function and a lambda. So when I pass more arguments to map, what it does is it takes an element from each of the inputs following the first function, and passes it to that function. So now I have a lambda that takes two arguments, num1 and num2. And evaluates the expression, that is a tuple containing num1 and num2, and then I have two lists afterwards. So it'll pull elements from each of those two lists to create these tuples, giving me a list of tuples. And that is, in fact, what I get, a list of tuples. I want to use another lambda here to get a list that only includes the minimum value from each of the input tuples. So I have a lambda that takes a single argument that I've called pair. And it takes the minimum of pair sub 0, and pair sub 1, and it operates over the list tups. When I map that, I'm going to get something I'm calling mins, and so it should be the minimums. Let's see if it actually works. And so if we look at the tuples, the first tuple was 0, 2 and the first element of my minimums list is 0. The next tuple was 1, 2, the second element in my minimums list is 1. Then we have 2, 4, we get a 2. Then we have 3, 2, we get a 2, 4, 6, we get a 4 and so on. Well, that seemed to work, okay? So by using map and lambda, I can write relatively complicated things in not too much code. It's pretty compact and efficient. I finally want to introduce you to another function like map that takes functions and operates on sequences of data called filter. The filter takes a function as its first argument and then a sequence, just like map did. But the difference here now is filter creates a list that is potentially shorter than the input sequence. It evaluates the function for each element in the input sequence, and if the function returns true, it adds it to the output list. If the function returns false, then it doesn't. So you can see here, I have a lambda that takes pair as an input, and it checks, is the second number less than the first number? So if that's true, filter is going to put that element into the output list. If it's not, then it's not going to. Let's run this. So you can see that my filtered tuples only ended up as a list of three tuples, even though the input had ten, all right? And if I expand this, you can see where they came from. All of the other tuples happen to have their first elements be smaller than the second element. Now you've seen how you can create and use anonymous functions. The key ideas here are you use the keyword lambda to define an anonymous function. You can have as many arguments as you'd like, then a colon and then a single Python expression, and the function evaluates that Python expression and returns its value. These are incredibly valuable when you simply need to create a quick function that is a single expression that you want to pass to another function. And so I showed you map and filter, both of which are very powerful functions for operating on sequences of data, that are ideal places to be using lambdas. Now there are many others, and we'll see one in the next video.