Python Learning Series · @onlysamarrr · Day 22 / 30

Day 22: lambda, map, filter

Day 22 teaches lambda, map and filter. lambda is a small one-line function without a name, map() runs a function on every item, and filter() keeps only items that pass a condition.

Phase: Intermediate · Reels: 106/450 – 110/450

Quick Answer

Day 22 teaches lambda, map and filter. lambda is a small one-line function without a name, map() runs a function on every item, and filter() keeps only items that pass a condition.

What You Will Learn

Code

double = lambda x: x * 2
print(double(5))
prices = [100, 250, 400]
off = list(map(lambda p: p * 0.9, prices))
print(off)
ev = list(filter(lambda n: n % 2 == 0, range(7)))
print(ev)
names = ["Al", "Riya", "Arhan"]
print(sorted(names, key=len))

Output

10
[90.0, 225.0, 360.0]
[0, 2, 4, 6]
['Al', 'Riya', 'Arhan']

Code Explained, Line by Line

  1. lambda x: x * 2 is a small anonymous function: x is the input, x * 2 is the output.
  2. map(lambda p: p * 0.9, prices) applies a 10% discount to every price.
  3. filter(lambda n: n % 2 == 0, ...) keeps only even numbers.
  4. sorted(names, key=len) sorts names by their length, using len as the key function.
  5. If a function needs a name or is long, use def instead of lambda: do not overuse lambda.
Common Mistake: Do not overuse lambda. For longer logic, write a named function with def so the code stays readable.

Real-Life Use

Discounts, data cleaning and sorting by a custom rule use map, filter and key functions.

Practice Task: Apply a discount to prices with map, and filter out even numbers.

Test Yourself — MCQs

Select (tick) an option, then tap Check answer to see instantly whether your answer is right or wrong, with an explanation. You can change your answer and check again.

Q1. What is lambda x: x * 2?

Q2. What does map() do?

Q3. What does filter() keep?

Today's 5 Reels (from the 90-Day Plan Excel)

106/450Conceptlambda function
107/450Live demomap aur filter demo
108/450Common mistakelambda ko overuse mat karo
109/450QuizQuiz: even numbers filter
110/450Interview / real lifeReal life: prices pe discount
← Day 21Day 23 →