1.2.34 · HinglishIntroduction to Programming (Python)

List comprehensions — `[expr for x in iterable if condition]`

1,680 words8 min readRead in English

1.2.34 · Coding › Introduction to Programming (Python)


YEH feature exist kyun karta hai?


EXACT anatomy kya hai?

Chaaron moving parts, reading order vs execution order mein:

Part Naam Role
expr output expression naye list mein kya JAATA hai (har item ke liye last mein compute hota hai)
for x in iterable the loop items kahan se aate hain (pehle run hota hai)
if condition filter (optional) x ko tabhi rakho jab True ho

![[1.2.34-List-comprehensions-—-`[expr-for-x-in-iterable-if-condition]`.png]]


Loop se ise HOW derive karein (yeh ek baar karo, kabhi memorise mat karo)


Aur worked examples



Flashcards

List comprehension ka general template kya hai?
[expr for x in iterable if condition]
Execution order mein kaunsa part pehle run hota hai: expr, for, ya if?
for (x pick karo), phir if (filter), phir expr (compute). Reading order ≠ execution order.
Filter if (koi else nahi) comprehension mein kahan jaata hai?
for ke baad: [x for x in xs if cond].
Conditional if/else (ternary) kahan jaata hai?
for se pehle, expr ke part ke roop mein: [a if cond else b for x in xs].
[x**2 for x in range(4)] ko loop ki tarah rewrite karo.
r=[]; for x in range(4): r.append(x**2)[0,1,4,9].
[c for row in grid for cell in row]-style multi-for ka matlab kya hai?
Nested loops; sabse left wala for outermost loop hai, top-to-bottom padho.
[n for n in [-1,2,-3] if n>0] ki value kya hai?
[2] (negatives filter ho jaate hain, items unchanged rehte hain).
Kya if condition part required hai?
Nahi, yeh optional hai; iske bina har item include hota hai.

Recall Feynman: ek 12-saal ke bachche ko explain karo

Socho ek seb ki conveyor belt. Ek list comprehension ek chhoti factory hai: belt har seb laati hai (for apple in belt), ek guard sade hue seb phenk deta hai (if apple is good), aur ek machine bache hue har seb ko apple juice mein badal deti hai (expr = juice). Ant mein tumhe apple juice ka ek naya box milta hai. Tumhe "khali box lo, belt ke paas jao, seb check karo, juice box mein daalo" chaar baar likhne ki zarurat nahi padi — tumne sirf factory ko ek sentence mein describe kiya aur Python ne use tumhare liye bana diya.


Connections

  • For loops — comprehension ek for loop ke upar sugar hai.
  • Conditional expressions (ternary)a if cond else b jo expr ke andar use hota hai.
  • Dictionary and set comprehensions — same idea [] ki jagah {} ke saath.
  • Generator expressions(...) lazy version jo poora list nahi banata.
  • map() and filter() — transform aur filter ke functional equivalents.
  • range() and iterables — jo for x in iterable slot ko feed karta hai.

Concept Map

collapses into

contains

contains

contains

supplies x

keeps x if True

appended to

written as

actually runs as

causes

fixes via loop rewrite

4-line for loop ritual

List comprehension one-liner

expr output

for x in iterable

if condition optional

New list

Reading order expr first

Execution order for-if-expr

Steel-man misread