1.2.32 · HinglishIntroduction to Programming (Python)

Lambda functions — anonymous, used with map - filter

1,637 words7 min readRead in English

1.2.32 · Coding › Introduction to Programming (Python)


WHAT is a lambda?

Toh yeh dono same function hain, bas alag tarike se bane hain:

def square(x):
    return x * x
 
square = lambda x: x * x      # identical behaviour

HOW map aur filter ise use karte hain

Dono (ek function, ek iterable) lete hain aur ek lazy iterator return karte hain (toh results dekhne ke liye list(...) mein wrap karo).

Figure — Lambda functions — anonymous, used with map - filter

DERIVATION — map/filter ko scratch se banana

WHY yeh karo? Agar tum inhe rebuild kar sako, toh yeh tumhare hain. Yeh disguise mein sirf loops hain.

def my_map(f, it):
    result = []
    for x in it:
        result.append(f(x))   # transform every element
    return result
 
def my_filter(f, it):
    result = []
    for x in it:
        if f(x):              # keep only when predicate true
            result.append(x)
    return result

Worked examples


Common mistakes (Steel-manned)


Active recall

Recall Quick self-test (pehle answers chhupao!)
  1. Lambda kya return karta hai, aur kaise? → Apne single expression ki value, automatically.
  2. list(map(lambda x: x+1, [0,9]))[1, 10]
  3. list(filter(lambda x: x>2, [1,2,3,4]))[3, 4]
  4. map ko list() mein kyun wrap karte hain? → Yeh ek lazy, one-pass iterator return karta hai.
  5. Kya lambda mein for loop aa sakta hai? → Nahi — sirf single expression.

Flashcards

What is a lambda function?
Ek anonymous (unnamed) function jo inline lambda args: expression ke roop mein likha jaata hai; expression ki value auto-return hoti hai.
How does a lambda return a value?
Implicitly — single expression HI return value hai; return use karna SyntaxError hai.
What does map(f, it) do?
f ko it ke har element pe apply karta hai, har x ke liye f(x) yield karta hai (lazy iterator).
What does filter(f, it) do?
it se har x tabhi yield karta hai jab f(x) truthy ho (ek predicate-based selector).
Why must you often wrap map/filter in list()?
Yeh lazy iterators return karte hain jo demand par compute karte hain aur ek pass mein exhaust ho jaate hain.
list(map(lambda x: x*2, [1,2,3])) equals?
[2, 4, 6]
list(filter(lambda x: x%2==0, [1,2,3,4])) equals?
[2, 4]
Can a lambda hold statements like loops or assignments?
Nahi — sirf ek single expression (ternary a if c else b allowed hai).
What does sorted(data, key=lambda p: p[1]) do?
data ko har item ke doosre element se compare karke sort karta hai.
What does filter(lambda x: x, items) return?
Sirf truthy items (0, '', None, [], False drop ho jaate hain).

Recall Feynman: 12-saal ke bache ko samjhao

Socho helpers ki ek vending machine hai. Aadat yeh hai ki tum ek helper banate ho, use naam dete ho, aur baad mein call karte ho. Lekin kabhi kabhi tumhe sirf ek baar ka helper chahiye — jaise "yeh number double karo" — aur use naam dena waste lagta hai. Lambda ek sticky-note helper hai: tum rule wahi likhte ho jahan tum use de rahe ho. map ek worker hai jo tumhara sticky-note rule leta hai aur ek basket mein har item par karta hai. filter ek bouncer hai jo tumhare rule se woh items rakhta hai jo pass karte hain aur baaki toss kar deta hai. Chhota rule, bade kaam.

Connections

  • Functions and def — lambda, named def ka anonymous cousin hai.
  • First-class functions — isliye functions arguments ke roop mein pass ho sakte hain.
  • List comprehensions — aksar zyada readable replacement: [x*2 for x in nums].
  • Iterators and lazy evaluation — isliye map/filter ko list() chahiye.
  • sorted and key functions — sort keys ke roop mein lambdas.
  • Higher-order functions — functions jo doosre functions lete/return karte hain.

Concept Map

is

body is

auto-returned

enables passing

supplied to

supplied to

transforms every element

selects truthy elements

materialise with

rebuilt as

rebuilt as

Lambda function

Anonymous no name

Single expression

Functions first-class objects

map f it

filter f it

Lazy iterator

Wrap in list

Just loops in disguise