A lambda function is a small, anonymous function defined with the keyword lambda. Unlike a regular function defined using def, a lambda is typically used for short, one-off operations that are not reused elsewhere. It can take any number of arguments but contains only one expression whose result is returned automatically.
- When you need a small function for a short period.
- To write quick, inline functions that are passed to higher-order functions (like
map(),filter(), andreduce()).
lambda arguments: expressionRegular function:
def square(x):
return x * x
print(square(5)) # Output: 25Lambda function:
square_lambda = lambda x: x * x
print(square_lambda(5)) # Output: 25Both approaches give the same result, but the lambda function is more concise.
add = lambda a, b: a + b
result = add(3, 7)
print(result) # Output: 10The filter() function constructs an iterator from elements of an iterable for which a function returns True. In simpler terms, it "filters out" items that do not meet a specific condition.
- When you have a list (or any iterable) and want to keep only the elements that satisfy a particular condition.
filter(function, iterable)- function: A function that returns
TrueorFalsefor each element. - iterable: A sequence (like a list, tuple, etc.) whose elements are to be filtered.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Using lambda to check if a number is even
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers) # Output: [2, 4, 6, 8, 10]Explanation:
- The lambda function
lambda x: x % 2 == 0returnsTruefor even numbers. filter()applies this lambda to every number in the list and returns only those numbers for which the lambda returnsTrue.
The map() function applies a given function to each item of an iterable (like a list) and returns an iterator with the results. It transforms each element according to the function provided.
- When you need to perform the same operation on every element in a list (or any iterable) without writing an explicit loop.
map(function, iterable)numbers = [1, 2, 3, 4, 5]
# Using lambda with map to square each number
squared_numbers = list(map(lambda x: x * x, numbers))
print(squared_numbers) # Output: [1, 4, 9, 16, 25]words = ["hello", "world", "python"]
# Using lambda with map to convert each string to uppercase
uppercased_words = list(map(lambda x: x.upper(), words))
print(uppercased_words) # Output: ['HELLO', 'WORLD', 'PYTHON']Explanation:
- In the first example, the lambda function computes the square of each number.
- In the second example, the lambda function converts each string to uppercase.
The reduce() function is used to apply a function cumulatively to the items of an iterable, reducing the iterable to a single value. It is part of the functools module in Python.
- When you need to aggregate or accumulate values from an iterable into one value (such as computing the sum or product of all elements).
from functools import reduce
reduce(function, iterable)from functools import reduce
numbers = [1, 2, 3, 4, 5]
# Using lambda with reduce to sum the numbers
sum_of_numbers = reduce(lambda x, y: x + y, numbers)
print(sum_of_numbers) # Output: 15Explanation:
- The lambda function takes two arguments
xandyand returns their sum. reduce()applies this lambda function cumulatively:- First, it computes
1 + 2which equals3. - Then it computes
3 + 3which equals6. - This process continues until the list is reduced to a single value (15).
- First, it computes
numbers = [10, 25, 30, 5, 50]
max_number = reduce(lambda x, y: x if x > y else y, numbers)
print(max_number) # Output: 50Explanation:
- The lambda function compares two numbers and returns the greater one.
reduce()uses this comparison to traverse the list, keeping track of the maximum number found so far.
-
Lambda Functions: Quick, anonymous functions that can be defined in one line.
Example:lambda x: x * x -
Filter Function: Filters elements in an iterable based on a condition.
Example:list(filter(lambda x: x % 2 == 0, numbers)) -
Map Function: Transforms each element in an iterable by applying a function.
Example:list(map(lambda x: x * x, numbers)) -
Reduce Function: Reduces an iterable to a single cumulative value.
Example:reduce(lambda x, y: x + y, numbers)
These tools are essential in functional programming in Python, providing concise ways to write transformations and data aggregations without explicitly using loops. Experiment with these examples and try modifying them to suit your needs!