Lambda Functions in Python



Python


In this article, we go over how to create a lambda function in Python.

So let's first go over what a lambda function is?

A lambda function is basically an express function when compared to a traditional Python function.

A traditional function in Python will be created with the def keyword and have other keywords probably such as return.

A lambda function is not created with these keywords. These keywords are implicit within a a lambda function (therefore, they do not need to be outrightly explicitly expressed).

A lambda function is a function created with a single line of code. All we have to do to create a lambda function is to use the lambda keyword followed parameter(s) of the function. We then follow this with a colon and then the operation we want done on the input (parameter) to the function.

A lambda function is a function that is anonymous, meaning that the function isn't explicitly created (or defined).

Instead, we simply put a lambda function where we need it without creating it in our code.

Anonymous functions save us coding because we don't have to actually create the function. It is simply anonymous and can be very useful in many cases.

So let's create a lambda function that gives the square of a number.

Before we show the lambda function, we will show the traditional Python created with the keywords def and that gives a return with the return keyword.



So first we create the traditional Python function that outputs the square of a number.

This function is called square()

We create this function with the def keyword followed by the name of the function, which is square().

Inside of this function we pass in 1 parameter, x.

This function returns the square of the parameter passed in.

We then print the square of the function with 7 passed in as the parameter.

We then get the output of 49 (72= 49).

We then create a lambda function of this traditional Python function.

We then create a lambda function and store this lambda function in squarenum.

To create this lambda function, we use the lambda keyword followed by the parameter of the function. We simply refer to the parameter as x. We then follow this with a colon. After this, we explicitly state the operation we would like to carry out on this which is the square of the number.

We then call the squarenum() function and pass in 9, which gives an output of 81.


Lambda Function with Multiple Parameters

Now we'll do one more lambda function, this time with multiple parameters.

So we're going to create a lambda function that adds 3 numbers together. The function has 3 parameters and add these 3 parameters together.

This is shown in the following code below.



So if there are multiple parameters, then after the lambda keyword, specify all parameters. After the colon is the operation that you would like to carry out on these parameters.

This is lambda functions with multiple parameters.


Lambda Function with an If-Else Statement

Now we will create a lambda function that uses an if-else statement.

The use of an if-else statement is a little different with a lambda function than with a traditional function.

We will create a lambda function that uses an if-else statement. This lambda function will take in 2 parameters, x and y. If the x value is larger, we will print out, "x is larger". If the y value is larger, we will print out, "y is larger".

This is shown in the following code below.



So first we have the traditional way of carrying out an if-else statement in a function, which you are more than likely familiar.

We then create the lambda function version.

We create a lambda function and assign it to the comparison variable.

So we use the lambda keyword and follow this with the parameters of the function. We then have our colon. Right after the colon, we must specify the return value. This is how if-else statements are different in lambda functions. Right after the colon, we specify the return value, 'x is greater' if x > y. Else, we return 'y is greater'

We then run the lambda function, passing in 2 for x and 6 for y. As a result, we get 'y is greater'

We then run the lambda function, passing in 8 for x and 1 for y. As a result, we get 'x is greater'

Sorting Objects Using a Lambda Function

In the next example, we will create a class and create objects in the class.

We will then sort the objects in a list according to one of its attributes using a lambda function.

We will create a class named Alligator and define our init method to have the parameters, self, color, length, mood, and age. We then create a few objects and place them inside of a list.

Using the sorted() function and a lambda function, we can sort the objects of the class according to any attribute.

Let's go over this now in code.



So in this code, we create a class named Alligator.

In this class, we define an __init__ method to have the parameters, self, color, length, mood, and age.

We then define an __repr__ method so that we can make an object of the class more human readable, which assists with debugging functions.

We then create 3 alligator objects and put them in a list.

We then create the varible, sorted_alligators_color and set it equal to sorted(alligators, key= lambda e:e.color)

This sorts the alligators list according to the color attribute.

Notice we've never defined this e function anywhere in our code. This is the power of lambda functions. They are anonymous functions. They are not explicitly defined. With this lambda function, the alligators list is sorted according to the color attribute.

So without defining a single function, we are able to simply call a function (a lambda function) to do a useful task, which is to sort a list of objects according to a certain attribute.

lambda is a reserved keyword in Python, so by using lamba before a name, this defines a lamba function.

So this is the basics of lambda functions in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...