What is Python Lambda function?

The lambda is an anonymous function that is nameless and generally used with higher order functions like map, filter etc.

Lambda with map example with filter example

The nameless or anonymous function can be created by using the keyword lambda. These are small functions that you may use when function objects are required.

For example, the map function requires a function to be passed along with iterator. There, you may pass a normal function created with def keyword. However, if the function is small and does not require any statements or annotations then you may use the lambda function there. I will show you with the examples in the following section.

How to use the lambda function?

The syntax of using the lambda function is:

lambda arguments: expression

The above syntax yields a function object. This behaves just like the normal function created with def keyword. For example:

def <lambda>(arguments):

return expression

Note: The anonymous functions that you create with lambda expression cannot contain statements or annotations.

See the following section for the examples of using the lambda function.

A simple example of lambda function and its equivalent to normal function

In this example, the square of a given argument is calculated by using the lambda function. The same is done by using a normal function with def keyword. See the code and output:

See online demo and code


Python lambda

You can see, the lambda function is given with an argument. As using the print function, the required parameter is passed and lambda function calculated the square of given integer value.

Similarly, another function is created with def keyword that calculated the square of given value and returned the result.

You can also see the difference that no return statement is used in the lambda function.

Now let me show you examples of lambda where it is generally used. That is, in map and filter functions that require a function to be passed.

An example of map with lambda

In this example, the map function, which is higher order function, is used that requires a function along with an iterator as the parameter. You may send one or more iterators like lists, tuple etc.

In the example, I will send two lists and the lambda function is used. The lambda function will sum the items of two integer lists and finally, we will display the returned list after using the map with lambda:

See online demo and code


lambda map

You can see, the two lists are passed as using the map function. The items of these lists are used in the lambda function where items from both lists are added. The map function returned a list that is displayed by using the print function.

For learning more about the map function, visit this map tutorial.

An example of calculating the Fahrenheit to Celsius using map and lambda

In this example, the Fahrenheit to Celsius formula is used in the lambda function. A list containing Fahrenheit values is provided to the map function while the required function in the map() is lambda function that converts Fahrenheit to Celsius.

The returned list is the Celsius temperature which is displayed by using the print function:

See online demo and code


An example of lambda with filter function

In the following example, the lambda function is used with the filter function. Two parameters are passed to the filter function to calculate the leap years:

  • lambda function calculating the leap year
  • a list of years

Each item of the list will be evaluated and only leap years will be filtered out and returned as a list. Have a look:

See online demo and code


lambda filter

You can see, the leap years are filtered out of the given list.