In Python, lambda functions are anonymous functions that are defined without a name. These functions are useful for small, one-off tasks and are often used as arguments to higher-order functions like map, filter, and reduce. This article explains lambda functions with examples.
The syntax for a lambda function is:
lambda arguments: expression
The lambda keyword is followed by arguments, a colon, and an expression that is evaluated and returned. Lambda functions can have any number of arguments but only one expression.
The following example demonstrates a lambda function to calculate the square of a number:
# Lambda function to calculate square square = lambda x: x * x # Using the lambda function result = square(5) print("The square of 5 is:", result)
Output:
The square of 5 is: 25
The map() function applies a given function to all items in an iterable. Lambda functions are often used with map() for concise code.
The following example demonstrates using a lambda function with map() to double each number in a list:
# List of numbers numbers = [1, 2, 3, 4, 5] # Using lambda with map doubled = map(lambda x: x * 2, numbers) # Converting the result to a list print("Doubled numbers:", list(doubled))
Output:
Doubled numbers: [2, 4, 6, 8, 10]
The filter() function filters elements of an iterable based on a condition. Lambda functions are useful for specifying the condition concisely.
The following example demonstrates using a lambda function with filter() to find even numbers in a list:
# List of numbers numbers = [1, 2, 3, 4, 5, 6] # Using lambda with filter even_numbers = filter(lambda x: x % 2 == 0, numbers) # Converting the result to a list print("Even numbers:", list(even_numbers))
Output:
Even numbers: [2, 4, 6]
The reduce() function from the functools module reduces an iterable to a single value by applying a function cumulatively. Lambda functions can simplify this process.
The following example demonstrates using a lambda function with reduce() to calculate the product of all numbers in a list:
from functools import reduce # List of numbers numbers = [1, 2, 3, 4, 5] # Using lambda with reduce product = reduce(lambda x, y: x * y, numbers) print("Product of numbers:", product)
Output:
Product of numbers: 120
Lambda functions can be used inline, making code more concise for small tasks.
The following example demonstrates a lambda function used as an argument to sorted() to sort a list of tuples by the second element:
# List of tuples pairs = [(1, 2), (3, 1), (5, 4), (2, 3)] # Using lambda with sorted sorted_pairs = sorted(pairs, key=lambda x: x[1]) print("Sorted pairs:", sorted_pairs)
Output:
Sorted pairs: [(3, 1), (1, 2), (2, 3), (5, 4)]
Lambda functions are a powerful feature in Python for writing concise, anonymous functions. They are particularly useful in functional programming with map(), filter(), and reduce(). While convenient, lambda functions should be used sparingly for clarity and maintainability. Experiment with these examples to understand the versatility of lambda functions.