Python Lambda Function

An infographic providing a visual guide to understanding Python lambda functions. Learn how to create and use anonymous functions in Python for concise and efficient code. The examples cover using lambda with map() and join() as well.

An introduction to Python Lambda functions

  • The lambda is an anonymous function that is nameless and generally used with higher-order functions like map, filter, etc.
  • 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 the iterator.
  • There, you may pass a normal function created with the def keyword. However, if the function is small and does not require any statements or annotations then you may use the lambda function there.
  • We will show you 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:

#An example of lambda function

#Using lamda function

cal_sq = lambda a: a*a

print ("The square using lambda: ", cal_sq(5))

#using normal function

def sq (z):

    return z*z

print ("The square using normal function: ", sq(5))

Result:

The square using lambda: 25
The square using normal function: 25

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

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

You can also see the difference in 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 a 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:

#An example of lambda function with map

a_List = [5, 10, 15, 20]

b_Lst2 = [30, 35, 40, 45]

c_List_sum = map(lambda num1,num2: num1+num2, a_List,b_Lst2)

print("The list after sum", list(c_List_sum))

Output:

The list after sum [35, 45, 55, 65]

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 Python 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:

Fahrenheit_List = [98, 102, 110, 125]

Celsius_List_sum = map(lambda F: ((5.0/9.0)*(F - 32)), Fahrenheit_List)

print("The Celsius Temp: ", list(Celsius_List_sum))

Output:Python-lambda-Far-Cel

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 on the list will be evaluated and only leap years will be filtered out and returned as a list. Have a look:

#An example of filter with lambda to get leap years

Year_list = [1992, 1994, 1996, 1998, 2000, 2003, 2004, 2008, 2010, 2012, 2014]

Leap_Years = list(filter(lambda leap: (leap%4 == 0) , Year_list))

print("The leap years in the list are: " ,Leap_Years)

Result:

The leap years in the list are: [1992, 1996, 2000, 2004, 2008, 2012]

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

Note: Leap year has two other conditions:

  • (i) Year is not divisible by 100. For example, 1900 is not a leap year, although it can be divided by 4.
  • (ii)Year can be divided by 400 is a leap year. For example, 2000 is a leap year as it can be divided by 400 (though it can be divided by 100 as well).
  • (iii)You need to incorporate these two conditions as well in the above code by using if..else if ladder etc.
Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!