Python Tutorial in Hindi #14 - Lambda Functions | Map, Filter & Reduce
Lambda Functions in Python | map(), filter(), reduce() Explained | Python Tutorial in Hindi
In this video by Mohit Decodes, we’ll explore Lambda Functions in Python — a powerful way to create small, anonymous functions in a single line.
✅ What You'll Learn:
What is a Lambda Function in Python?
Difference between Lambda and Normal Functions
Lambda with map(), filter(), and reduce()
Real-life examples to understand practical usage
Best practices for using lambda in clean code
# Python Tutorial #14 – Lambda Functions | Map, Filter & Reduce
# Author: Mohit Decodes
# ------------------------------
# 🧩 Example 1: Normal vs Lambda Function
# ------------------------------
# Normal Function
def add(a, b):
return a + b
print("Normal Function Result:", add(5, 3))
# Lambda Function
sum_lambda = lambda a, b: a + b
print("Lambda Function Result:", sum_lambda(5, 3))
# ------------------------------
# 🧩 Example 2: Lambda Function to Find Square
# ------------------------------
square = lambda x: x * x
print("Square of 6:", square(6))
# ------------------------------
# 🧩 Example 3: Lambda Function with if-else
# ------------------------------
max_num = lambda a, b: a if a > b else b
print("Greater Number is:", max_num(20, 15))
# ------------------------------
# 🧩 Example 4: Lambda Inside a Normal Function
# ------------------------------
def myfunc(n):
return lambda a: a * n
doubler = myfunc(2)
tripler = myfunc(3)
print("Double of 5:", doubler(5))
print("Triple of 5:", tripler(5))
# ------------------------------
# 🧩 Example 5: Using map() with Lambda
# ------------------------------
numbers = [1, 2, 3, 4, 5]
# Square each number using map
squared = list(map(lambda x: x ** 2, numbers))
print("Squared Numbers:", squared)
# Convert list of numbers to strings
as_strings = list(map(lambda x: str(x), numbers))
print("Numbers as Strings:", as_strings)
# ------------------------------
# 🧩 Example 6: Using filter() with Lambda
# ------------------------------
# Filter even numbers
even_nums = list(filter(lambda x: x % 2 == 0, numbers))
print("Even Numbers:", even_nums)
# Filter numbers greater than 3
greater_than_three = list(filter(lambda x: x > 3, numbers))
print("Numbers > 3:", greater_than_three)
# ------------------------------
# 🧩 Example 7: Using reduce() with Lambda
# ------------------------------
from functools import reduce
# Find product of all numbers
product = reduce(lambda x, y: x * y, numbers)
print("Product of all numbers:", product)
# Sum of all numbers
total = reduce(lambda x, y: x + y, numbers)
print("Sum of all numbers:", total)
# ------------------------------
# 🧩 Example 8: Combined Example
# ------------------------------
# Double the even numbers and find their sum
nums = [1, 2, 3, 4, 5, 6]
even_nums = list(filter(lambda x: x % 2 == 0, nums))
doubled = list(map(lambda x: x * 2, even_nums))
sum_even = reduce(lambda x, y: x + y, doubled)
print("Even Numbers:", even_nums)
print("Doubled Values:", doubled)
print("Sum of Doubled Even Numbers:", sum_even)
# ------------------------------
# 🧩 Example 9: Practical Example
# ------------------------------
students = [
{"name": "Mohit", "score": 85},
{"name": "Ravi", "score": 60},
{"name": "Amit", "score": 75},
{"name": "Rohan", "score": 90}
]
# Get names of students who scored above 70
top_students = list(map(lambda s: s["name"], filter(lambda s: s["score"] > 70, students)))
print("Top Students:", top_students)