 Company-Specific Courses
Free Resources Edit Edit

# Lambda Functions In Python (Anonymous Functions) | FACE Prep

Published on 09 Mar 2020

Lambda functions in Python are also known as Anonymous functions (functions without a name). These functions replace the traditional user-defined functions having two or more lines with a simple one-line function.

Consider the below program that prints the square of a number using a function.

def square(a):return a * a

#main() function
res = square(6)
print(res)


Output:
36



Here, a * a is a single expression. To execute this single expression, we had to write a function definition with the 'def' keyword, function name, parameters, and a return statement. But all these are not required if we implement the same using the lambda function.

So, let us move ahead and understand more about how these functions can be used.

## Syntax for Lambda Functions in Python

Below is the syntax for the lambda function in Python.

lambda argument (s): expression



Just as you define a user-defined function using 'def' keyword, you need to define a lambda function using 'lambda' keyword. A lambda function can have 'n' number of arguments but only one expression.

Let us rewrite the above code using the lambda function and see how it works.

f = lambda a: a * a #Lambda function

#main() function
res = f(6)
print(res)



## Explanation: Next, let us dive deep to understand where these lambda functions are used.

## Usage of lambda function in Python

These functions are used for creating small, one-line anonymous functions without much coding effort. The real usage of the lambda functions can be known only when it is used as part of another function. Also, Lambda functions are very useful when working with map() and filter() functions.

Now, let us see how to use the lambda functions with map() and filter() functions.

## a) Lambda function with map()

map() function executes another user-defined function for each element in an iterator. To do this it takes two arguments. One is user-defined function and another is an iterator. Below is the example, in which map() function executes the user-defined function len() for two elements of the iterator.

def func(a):return len(a)

#main() function
list1 = ["FACE Prep", "Python"]
res = map(func, list1)
print(list(res))



Output:
[9, 6]


Now, let us rewrite the same code using the lambda function.

list1 = ["FACE Prep", "Python"]
res = map(lambda a: len(a), list1)
print(list(res))


Output:
[9, 6]


## b) Lambda function with filter()

filter() function filters the given iterator with the help of a function. Consider the below-given example where filter() function filters and prints even numbers of the given iterator using a user-defined function.

def func(a):if(a % 2 == 0):
return a

#main() function
list1 = [12, 5, 18, 22, 97, 44]
res = filter(func, list1)
print(list(res))


Output:
[12, 18, 22, 44]


Below is the same above code implemented using the lambda function.

list1 = [12, 5, 18, 22, 97, 44]
res = list(filter(lambda a: (a % 2 == 0) , list1))
print(res)


Output:
[12, 18, 22, 44]


You can observe that the number of lines of code implemented using the lambda function is less when compared to the traditional way of coding.

Let us see more lambda functions examples to understand them better.

## Python Lambda Functions Examples

First, let us see how to write a lambda function to add two numbers. As we already discussed, the lambda function can have more than one argument. To add two numbers, we need two arguments. Those two arguments have to be written after the 'lambda' keyword as comma-separated without any round brackets.

f = lambda a, b: a + b

#main() function
res = f(6, 4)
print(res)



Output:
10


Consider the below-given code where the built-in function 'format' formats the given input number to a hexadecimal value.

#Lambda function to format the given input number to a hexadecimal value
f = lambda a: format(a, 'x')

#main() function
res = f(12)
print(res)

Output:
c


## Lambda Function FAQs

1) What are lambda functions in Python?

Lambda functions are also called Anonymous functions which are used to write small, one-line functions. These functions are mainly implemented along with built-in functions in Python.

2) Are lambda functions faster in Python?

Yes, creating a function with lambda is faster than creating it using the traditional way. But, you need to be careful while writing the lambda functions.  