1. lambda Function
Definition:
A lambda function is a small anonymous function used for simple one-line operations.
It can take any number of arguments but has only one expression, which is implicitly returned.
Syntax:
lambda arguments: expression
Examples:
Add Two Numbers
add = lambda a, b: a + b
print(add(2, 3)) # Output: 5
Square a Number
square = lambda x: x ** 2
print(square(4)) # Output: 16
Maximum of Two Numbers
maximum = lambda a, b: a if a > b else b
print(maximum(5, 9)) # Output: 9
Even/Odd Checker
check = lambda x: "Even" if x % 2 == 0 else "Odd"
print(check(7)) # Output: Odd
Concatenate Strings
concat = lambda s1, s2: s1 + " " + s2
print(concat("Hello", "World")) # Output: Hello World
Cube of a Number
cube = lambda x: x**3
print(cube(3)) # Output: 27
2. map() Function
Definition:
The map() function applies a given function to each element in an iterable (like a list) and returns a new map object (an iterator).
Syntax:
map(function, iterable)
Examples:
Square Elements in List
nums = [1, 2, 3]
squares = list(map(lambda x: x**2, nums))
print(squares) # Output: [1, 4, 9]
Uppercase Strings
words = ["hello", "world"]
result = list(map(lambda word: word.upper(), words))
print(result) # Output: ['HELLO', 'WORLD']
Convert Temperatures Celsius to Fahrenheit
celsius = [0, 10, 20, 30]
fahrenheit = list(map(lambda c: (9/5)*c + 32, celsius))
print(fahrenheit) # Output: [32.0, 50.0, 68.0, 86.0]
Add Corresponding Elements of Two Lists
a = [1, 2, 3]
b = [4, 5, 6]
added = list(map(lambda x, y: x + y, a, b))
print(added) # Output: [5, 7, 9]
3. filter() Function
Definition:
The filter() function filters elements from an iterable based on a function that returns True or False. It returns only those elements where the function returns True.
Syntax:
filter(function, iterable)
Examples:
Filter Even Numbers
nums = [1, 2, 3, 4]
even = list(filter(lambda x: x % 2 == 0, nums))
print(even) # Output: [2, 4]
Filter Long Words
words = ["hi", "hello", "world", "yo"]
long_words = list(filter(lambda w: len(w) > 3, words))
print(long_words) # Output: ['hello', 'world']
Filter Positive Numbers
nums = [-5, 3, -1, 7, 0]
positive = list(filter(lambda x: x > 0, nums))
print(positive) # Output: [3, 7]
Filter Non-Empty Strings
strings = ["", "apple", "", "banana", " "]
non_empty = list(filter(lambda s: s.strip() != "", strings))
print(non_empty) # Output: ['apple', 'banana']
4. reduce() Function
Definition:
The reduce() function applies a function cumulatively to the items of an iterable, reducing it to a single cumulative value.
Note: You need to import reduce from the functools module.
Syntax:
from functools import reduce
reduce(function, iterable)
Examples:
Multiply All Numbers
from functools import reduce
nums = [1, 2, 3, 4]
product = reduce(lambda x, y: x * y, nums)
print(product) # Output: 24
Find Maximum in a List
from functools import reduce
nums = [3, 7, 2, 9, 5]
max_val = reduce(lambda a, b: a if a > b else b, nums)
print(max_val) # Output: 9
Sum of All Numbers
from functools import reduce
nums = [10, 20, 30, 40]
total = reduce(lambda x, y: x + y, nums)
print(total) # Output: 100
Flatten a List of Lists
from functools import reduce
lists = [[1, 2], [3, 4], [5, 6]]
flattened = reduce(lambda a, b: a + b, lists)
print(flattened) # Output: [1, 2, 3, 4, 5, 6]
5. Compare Normal Function vs Lambda Function
Feature |
Normal Function |
Lambda Function |
Definition |
Named function defined using def |
Anonymous function defined with lambda keyword |
Syntax |
def func(args): return expression |
lambda args: expression |
Function Body |
Can contain multiple statements |
Single expression only |
Use Case |
Suitable for complex logic and reuse |
Best for small, simple functions often used temporarily |
Name |
Has a function name |
Anonymous (no name unless assigned) |
Return Statement |
Explicit return required |
Implicit return of the expression |
Examples:
Normal Function
def add(a, b):
return a + b
print(add(3, 4)) # Output: 7
Equivalent Lambda Function
add = lambda a, b: a + b
print(add(3, 4)) # Output: 7
Normal Function with Multiple Statements
def process(x):
print("Processing", x)
return x * 2
print(process(5)) # Output: Processing 5 \n 10
Note: Lambda cannot have multiple statements or print.
Final Comparison Summary
Function |
Description |
Returns |
Common Use |
lambda |
Anonymous one-line function |
Function object |
Quick, short expressions |
map() |
Apply a function to every element |
Map object |
Transform list values |
filter() |
Filter elements meeting a condition |
Filter object |
Keep only matching values |
reduce() |
Reduce list to a single cumulative value |
Single value |
Totals, products, max/min etc. |
Conclusion
Use lambda when you need short, anonymous functions for quick one-line operations.
Use normal functions for complex logic, multi-line processing, and when function reuse is needed.
Use map() to apply transformations to all elements in an iterable.
Use filter() to select items from an iterable that satisfy a condition.
Use reduce() to combine all elements in an iterable into a single cumulative result.
Happy Learning Continue the Lessons 🙂