Lambda Function in Python
Hey there! In this guide, we'll explore Lambda Function in Python.A lambda function can take n number of arguments at a time. But it returns only one argument at a time. Its useful for simple, quick operations and can replace small function definitions where full def functions might feel excessive. Let's dive it!
Lambda Function​
Lambda functions in Python, also known as anonymous
functions, are small, one-line functions defined with the lambda
keyword.
1. Syntax​
lambda arguments: expression
This function accepts any count of inputs but only evaluates and returns one expression. That means it takes many inputs but returns only one output.
2. Why Use Lambda Instead of Def?​
Using lambda instead of def can be beneficial in certain situations where brevity and inline functionality are preferred.
The code defines a Square function using both the def
keyword and a lambda
function.
Example:
#using def
def square(num):
return num*num
num1=2
print(square(num1)) #Output:4
#using lambda
lambda_square=lambda x:x*x
print(lambda_square(3)) #Output:9
Both of these give the correct output for the square of a
given number without employing Lambda. However, while
using def
we were also required to use the return
keyword to provide the output from wherever the function
was invoked after being executed.Instead of a "return"
statement, Lambda definitions always include a statement
given at output.
The convenience of lambda functions lies in their flexibility.We dont need to allocate a lambda expression to a variable because we can put it at any place a function is required.
3. Practical Use cases of Lambda Function​
-
Using Lambda Function with filter()​
A program to filter a list of integers that are divisible by 3 using Lambda function from given list of integers.
numbers = [10, 15, 22, 33, 42, 55, 60, 71, 81]
divisible_by_3 = list(filter(lambda x: x % 3 == 0, numbers))
print("Numbers divisible by 3:", divisible_by_3)
#Output: Numbers divisible by 3: [15, 33, 42, 60, 81]
-
Using Lambda Function with map()​
A program to cube every number in a given list of integers using Lambda function.
input=[ 1, 2, 3, 4, 5]
cube=list(map(lambda n:n**3,input))
print("Cube every number of the input is",cube)
#Output: Cube every number of the input is [1, 8, 27, 64, 125]
-
Using Lambda Function with reduce()​
A program to calculate the product of all numbers in a list
from functools import reduce
numbers = [1, 2, 3, 4]
product = reduce(lambda x, y: x * y, numbers)
print(product) # Output: 24
-
Using Lambda Function in List Comprehension​
A program to filter a list of integers that are divisible by 3 using Lambda function from given list of integers.
numbers = [10, 15, 22, 33, 42, 55, 60, 71, 81]
divisible_by_3 = [x for x in numbers if (lambda x: x % 3 == 0)(x)]
print("Numbers divisible by 3:", divisible_by_3)
#Output: Numbers divisible by 3: [15, 33, 42, 60, 81]
-
Using Lambda Function with if-else​
A Program using lambda function to check if a number is even or odd
even_or_odd = lambda x: "Even" if x % 2 == 0 else "Odd"
print(even_or_odd(10)) # Output: Even
print(even_or_odd(7)) # Output: Odd
-
Using Lambda Function with Multiple Statements​
A Program using lambda function to for sorting the sublists and find the third largest number from every sublist
my_List = [ [3, 5, 8, 6], [23, 54, 12, 87], [1, 2, 4, 12, 5] ]
sort_List = lambda num: [sorted(n) for n in num]
#sorted list : [[3, 5, 6, 8],[12, 23, 54, 87],[1, 2, 4, 5, 12]]
third_Largest = lambda num, func: [l[-3] for l in func(num) if len(l) >= 3]
result = third_Largest(my_List, sort_List)
print('The third largest number from every sublist is:', result)
#Output:The third largest number from every sublist is: [5, 23, 4]
4. Conclusion​
Lambda functions in Python provide a concise way to create small, anonymous functions for temporary use, enhancing readability and enabling
functional programming. While more intricate logic, traditional def
functions are preferred for better maintainability, lambda
is ideal
for simple tasks, they should be used judiciously, as complex expressions can reduce clarity. Undersatnding corret usage of both the function
can make your code clean and effective.
Happy Learning!