They allow you to write clean, concise, and expressive code that focuses on what to do, rather than how to do it.
Lambda (Anonymous Functions)
A lambda function is a small, anonymous function โ meaning it has no name. It is defined using the lambda keyword and can take any number of arguments, but it can only contain a single expression.Syntax:
lambda arguments: expression
Example:
square = lambda x: x * x
print(square(5)) # Output: 25
Another example:
add = lambda a, b: a + b
print(add(3, 7)) # Output: 10
Lambda functions are commonly used when you need a simple function for a short duration โ especially as arguments to other functions.
map()
The map() function applies a given function to each item in an iterable (like a list or tuple) and returns a map object (which can be converted into a list).Syntax:
map(function, iterable)
Example:
numbers = [1, 2, 3, 4, 5]
squared = map(lambda x: x**2, numbers)
print(list(squared)) # Output: [1, 4, 9, 16, 25]
In this example, the lambda function squares each element of the list.
filter()
The filter() function filters elements from an iterable based on a condition provided by a function. It returns only those elements for which the function returns True.Syntax:
filter(function, iterable)
Example:
numbers = [10, 15, 20, 25, 30]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
print(list(even_numbers)) # Output: [10, 20, 30]
Here, the lambda function checks if each number is even, and filter() returns only those that satisfy the condition.
reduce()
The reduce() function (available in the functools module) repeatedly applies a function to the items of a sequence, reducing it to a single cumulative value.Syntax:
from functools import reduce
reduce(function, iterable)
Example:
from functools import reduce
numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, numbers)
print(product) # Output: 120
Here, reduce() multiplies all elements together: (((1 * 2) * 3) * 4) * 5 = 120
When to Use map(), filter(), and reduce()
| Function | Purpose | Returns |
|---|---|---|
| map() | Applies a function to all elements | Transformed sequence |
| filter() | Selects elements meeting a condition | Filtered sequence |
| reduce() | Aggregates all elements into one value | Single cumulative result |
Advantages of Using Lambda and Functional Tools
- Concise Code โ Avoids defining full functions for simple tasks.- Improved Readability โ Express transformations in a single line.
- Efficient โ Ideal for operations on sequences and collections.
- Functional Programming โ Promotes immutability and stateless computations.
Summary
Lambda and higher-order functions like map(), filter(), and reduce() give you elegant ways to process data in Python. They eliminate boilerplate loops and make code more expressive, especially for transformations and aggregations.| Concept | Description |
|---|---|
| lambda | Creates small, unnamed (anonymous) functions |
| map() | Applies a function to every element in a sequence |
| filter() | Returns elements that satisfy a condition |
| reduce() | Combines elements into a single result |
| functools | Module containing reduce() and other functional tools |