LAMBDA FUNCTION IN PYTHON
Python Lambda Function are anonymous function (i.e., defined without a name) that can take any number of arguments but, unlike normal functions, evaluates and returns only one expression.
Python Lambda Function Syntax
lambda arguments : expression
lambda: The keyword to define the function.
arguments: A comma-separated list of input parameters (like in a regular function).
expression: A single expression that is evaluated and returned.
Example 1 :
This code demonstrates different lambda functions:
- Addition by 2 (x+2).
- Square of a number (x**2).
- Adding 10 (a+10).
- Multiplying by 2 (a*2).
Each lambda function performs a mathematical operation and returns the expected result.
Example 2:
Lambda functions can take any number of arguments
This code demonstrates various lambda functions:
- Basic arithmetic (2*3+2+3 = 11).
- Multiplication with addition (a*b+a+b).
- Power function (base**exp+addval).
- String uppercase conversion (a.upper()).
- Multiplication of two numbers (a*b).
Each lambda function performs a simple operation and returns the result.
Why Use Lambda Functions?
The power of lambda is better shown when you use them as an anonymous function inside another function.
Say you have a function definition that takes one argument, and that argument will be multiplied with an unknown number.
Step-by-step Explanation:
Function Definition (double(a))
- This function takes a parameter a .
- It returns a lambda function that takes two parameters, n and b.
- The lambda function performs the calculation: (a×n)+a−b(a \times n) + a — b(a×n)+a−b
Calling double(5)
- When double(5) is called, a is assigned the value 5.
- The function returns a lambda function where a = 5.
Lambda Function Execution (dum(2,1))
- dum now holds the lambda function with a = 5
- Calling dum(2,1) passes n = 5 and b =1 into the lambda function.
Substituting values into the formula:
Get Jennifer’s stories in your inbox
Join Medium for free to get updates from this writer.
(5×2)+5−1(5 \times 2) + 5–1(5×2)+5−1 10+5−1=1410 + 5–1 = 1410+5−1=14
Final Output:
The result printed is 14.
Lambda with Condition Checking
A lambda function can include conditions using if statements.
This lambda function checks whether a number is even or odd. If x is divisible by 2, it returns ‘even’ ; otherwise, it returns ‘odd’ . In this case, conditional_lambda(4) evaluates to ‘even’ , which is printed as output.
Lambda with Nested If Condition
The lambda function will return a value for every validated input. Here, if block will be returned when the condition is true, and else block will be returned when the condition is false.
This lambda function categorizes a number as ‘Positive’ if greater than 0, ‘zero’ if equal to 0, ‘close to zero’ if between -5 and 0, and ‘negative’ if less than -5. Since 5 > 0 , the output is ‘positive’.
Lambda with List Comprehension
Combining lambda with list comprehension enables us to apply transformations to data in a concise way.
This code creates a list of lambda functions that multiply an argument by 10, using x from range(1,5) . When calling each function, it multiplies x (last value in range, 4) by 10 , giving outputs 10,20,30,40 .Sorting with lambda function.
Now will Perform Map in Lambda Function
Map in Python is a function that works as an iterator to return a result after applying a function to every item of an iterable.
Syntax : map(func,*iterables)
Here in example 1 we have num a list , I want square of all the numbers in the list so here I want to apply this function to every item of an iterable so I used Map function here.
Now will understand Filter in Lambda Function
filter out all the elements of a sequence.
Syntax : filter(function,iterable)
Here from 1 to 100 I want to extract only even numbers so , I need to apply it in every item so here I used filter function to filter the list number according to the given condition.
Reduce Function
Reduce returns a single output value from a sequence data structure because it reduces the elements by applying a given function it is used to perform some aggregate function.
Syntax: reduce(function, iterable)
Explanation:
- The lambda multiplies two numbers at a time.
- reduce() applies this operation across the list.
When to use lambda function?
Lambda functions are suitable for:
Simple operations: When you need to perform simple operations or calculations.
Anonymous functions: When you need a function temporarily or in one place and don’t want to define a named function using def.
Functional programming: When working with functions as first-class citizens, such as in map(), filter(), or sorted() functions where a function is passed as an argument.







