Lambda Functions
A lambda function is a small anonymous function.
A lambda function is an anonymous, single-line function that you can pass directly as an
argument to another function without needing to give it a formal name using standard
definition keywords. This technique is widely used in functional programming to inject
concise logic or behavior directly where it is needed. [1, 2, 3, 4, 5]
A lambda function can take any number of arguments, but can only have
one expression.
In Python, lambda functions are restricted to a single expression. They are frequently used with
built-in functions like map(), filter(), or sorted()
Syntax
lambda arguments : expression
Add 10 to argument a, and return the result:
x = lambda a : a + 10
print(x(5))
outpur :
15
Multiply argument a with argument b and return the result:
x = lambda a, b: a * b
print(x(5, 6))
output:
30
Program 2:
# A list of numbers
numbers = [1, 2, 3, 4, 5]
# Using lambda as an argument in map() to double each number
doubled = list(map(lambda x: x * 2, numbers))
print(doubled)
# Output: [2, 4, 6, 8, 10]
# Using lambda as an argument in filter() to extract odd numbers
odds = list(filter(lambda x: x % 2 != 0, numbers))
print(odds)
# Output: [1, 3, 5]
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:
Example:
def myfunc(n):
return lambda a : a * n
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
print(mydoubler(11))
output :
22
Example:
def myfunc(n):
return lambda a : a * n
mytripler = myfunc(3)
print(mytripler(11))
output:
33
Example:
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
mytripler = myfunc(3)
print(mydoubler(11))
print(mytripler(11))
output:
22
33
Lambda with Built-in Functions
Lambda functions are commonly used with built-in functions
like map(), filter(), and sorted().
Using Lambda with map()
The map() function applies a function to every item in an iterable:
Example:
numbers = [1, 2, 3, 4, 5]
doubled = list(map(lambda x: x * 2, numbers))
print(doubled)
output:
[2,4,6,8,10]
Using Lambda with filter()
The filter() function creates a list of items for which a function
returns True:
Example:
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
odd_numbers = list(filter(lambda x: x % 2 != 0, numbers))
print(odd_numbers)
output:
[1,3,5,7]
The map() function in Python executes a specified function for each item in an iterable (like a
list, tuple, or dictionary) and returns a lazy iterator (a map object).
MAP( )
map(function, iterable, ...)
function: The operation applied to every element.
iterable: One or more collections (lists, tuples, sets, etc.) containing the data.
Because map() uses lazy evaluation, it does not compute results until you loop through them or
convert them into a data structure like a list
# Function to square a number
def square(n):
return n * n
numbers = [1, 2, 3, 4]
# Apply the function to the list
result = map(square, numbers)
# Convert the map object to a list to view results
print(list(result))
# Output: [1, 4, 9, 16]
Example :
Calculate the length of each word in the tuple:
def myfunc(n):
return len(n)
x = map(myfunc, ('apple', 'banana', 'cherry'))
output:
[5,6,6]