1.2.34 · D1 · HinglishIntroduction to Programming (Python)

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

4,002 words18 min read↑ Read in English

1.2.34 · D1 · Coding › Introduction to Programming (Python) › [[1.2.34 List comprehensions — `[expr for x in iterable if condition]`|List comprehensions — `[expr for x in iterable if condition]]]

Before tum us one-liner ko padh sako, tumhe un chhote pieces mein fluent hona chahiye jinse woh bana hai. Is page mein yeh assume kiya gaya hai ki tum kuch nahi jaante, aur har symbol earn kiya jaata hai, ek ek karke, us order mein jisme woh ek doosre pe depend karte hain. Hum full recipe sirf final section mein assemble karenge, jab har part ka ek meaning ho jaayega.


0a. Value kya hoti hai? Numbers, strings, aur =

Picture: ek value ek physical object hai — ek coin (5), ek likha hua word ("apple") — jise tum hold kar sakte ho, pass around kar sakte ho, ya ek box mein daal sakte ho.


0b. Square brackets [ ] aur comma ,list aakhir hoti kya hai

Picture: numbered lockers ki ek row imagine karo, har pair ke beech ek patla divider (comma). Locker 0 mein pehla item, locker 1 mein doosra, aur aage bhi isi tarah (Python zero se count karna shuru karta hai, ek se nahi — yeh yaad rakho).

Figure — List comprehensions — `[expr for x in iterable if condition]`
Figure s01 — ek list ko numbered lockers ki row ki tarah draw kiya gaya hai. Har locker ek item rakhta hai; patle dividers commas hain; har locker ke neeche index label 0 se count karta hai.


1. Variables aur naam x — ek sticky label

Picture: ek luggage tag. Tag kehta hai x; ise ek suitcase (value) se doosri pe move kiya ja sakta hai. Loop ke dauran, tag x har item pe re-stick hota jaata hai.


2. iterable, range(), aur "generator" ka matlab — items ka source

Picture: ek Pez dispenser ya conveyor belt — tum sab kuch ek saath nahi lete, tum next item lete ho, phir next.

Figure — List comprehensions — `[expr for x in iterable if condition]`
Figure s02 — range(4) ek dispenser ke roop mein jo 0, 1, 2, 3 order mein hand out karta hai; greyed-out 4 dikhata hai ki stop value kabhi produce nahi hoti.

range() and iterables dekho for slot ko feed karne wali cheez ki full story ke liye.


3. for loop, colon :, aur indentation — engine

Picture: colon ek doorway sign hai jisme likha hai "loop body this way ↓", aur indentation woh walled-off room hai jis taraf sign point karta hai. Ek factory worker belt se har item ek ek karke uthata hai, us pe indented kaam karta hai, aur agla item uthata hai.

Figure — List comprehensions — `[expr for x in iterable if condition]`
Figure s03 — for loop ek conveyor belt ke roop mein: items 0, 1, 2 ek ek karke aate hain, loop variable x baarimaar inme se har ek ban jaata hai, aur body har item ke liye ek baar run karti hai.


4. .append() aur empty list [] — list kaise badhti hai

Picture: box ke peechhe ek aur apple daalte jaana har baar.


5. Boolean values aur condition — True / False

Picture: ek light switch ya ek yes/no gate. True = gate khula (ise through jaane do), False = gate band (ise phenk do).


6. Expressions vs statements, aur ternary a if cond else b

Picture: ek expression ek vending machine hai — cheezein daalo, ek value nikalti hai. Ek statement ek light switch hai — woh act karta hai, lekin tum iski result ko "hold" nahi kar sakte.

Figure — List comprehensions — `[expr for x in iterable if condition]`
Figure s04 — left: ek expression (vending machine) hamesha ek value hand back karta hai, toh woh output slot fill kar sakta hai; right: ek statement (switch) act karta hai lekin kuch return nahi karta, toh woh nahi kar sakta.


7. ** aur len() — examples mein transform tools


Sab kuch jod ke: [x**2 for x in range(10) if x % 2 == 0] padho

Ab har piece earned hai, toh hum finally recipe [expr for x in iterable if condition] assemble kar sakte hain aur parent ka flagship example left to right padh sakte hain, har part ko naam dete hue:

Fragment Ab tum jaante ho iska matlab Built in
[ ... ] aur iski , ek nayi list of values banana, comma-separated §0a, §0b
x**2 expr: ek expression jo ek value produce karta hai §6, §7
for x in range(10) ek iterable pe loop, x har item ko re-label karta hai §1, §2, §3
if x % 2 == 0 Boolean guard sirf survivors ko rakhta hai §5

Tum ise ek English sentence ki tarah padh sakte ho: "value x**2, har x ke liye 0 se 9 tak, lekin sirf tab jab x even ho."


Prerequisite map

value literal and = assignment

Square brackets and comma = a list

Variables a re-stickable name x

Iterables range and generator

colon and indentation block

The for loop engine

empty list and append

Booleans and conditions

power ** and len

Expression statement and ternary

List comprehension


Equipment checklist

Khud test karo — right side cover karo aur reveal karne se pehle har cheez zor se bolo.

Value kya hai, aur literal kya hai?
Value data ka ek piece hai (ek number, ek string); literal woh value hai jo code mein directly likhi ho, jaise 5 ya "apple".
Ek single = kya karta hai?
Yeh assign karta hai — ek naam ko ek value pe stick karta hai, jaise x = 5. Yeh ek command hai, koi question nahi.
Comma-separated values ke around [ ] kya banata hai?
Ek ordered list — values ka ek box, jisme har ek ki fixed position hoti hai, 0 se count karte hue.
Kya comma kisi item ki value ka hissa hai?
Nahi — yeh pure syntax hai jo ek item ko doosre se alag karti hai.
for x in ... mein x fixed hai ya changing?
Changing — yeh har item se re-attach hota hai baarimaar, har pass pe ek.
Kya ek list comprehension ka loop variable x outer scope mein leak karta hai?
Nahi — Python 3 mein yeh comprehension ke private scope mein rehta hai aur bahar ke same naam wale x ko overwrite nahi karta.
range(4) kya produce karta hai?
0, 1, 2, 3 — 0 se shuru, 4 se pehle rok deta hai.
Kya range ek generator hai?
Nahi — yeh ek reusable sequence object hai; tum ise do baar loop kar sakte ho, index kar sakte ho, ya iski length le sakte ho.
Generator ek phrase mein kya hai?
Ek one-shot, lazy iterable — yeh har item tabhi banata hai jab maanga jaaye aur ek pass ke baad used up ho jaata hai.
Iterable ek phrase mein kya hai?
Kuch bhi jisse tum items ek ek karke, order mein, nikaal sako.
for line ke end mein colon : kya karta hai?
Yeh announce karta hai ki code ka ek block (loop body) follow karta hai; iske neeche indented lines wahi block hain.
for loop kya karta hai?
Apna indented body ek iterable ke har item ke liye ek baar run karta hai, loop variable ko us item pe set karke.
r=[] se shuru karke, r.append(7) kya karta hai?
7 ko end mein add karta hai, [7] banata hai.
Ek Boolean ke do values kya hote hain?
Sirf True ya False.
= aur == mein kya fark hai?
= ek naam ko ek value assign karta hai; == poochhat hai "kya yeh equal hain?" aur ek Boolean return karta hai.
x % 2 == 0 kya test karta hai, aur negatives ke saath % ka kya caveat hai?
Whether x even hai; % divisor ka sign leta hai, toh jaise -7 % 3 hai 2, na ki -1.
Ternary a if cond else b ki syntax aur evaluation order kya hai?
Python pehle cond evaluate karta hai; agar True toh poora expression a yield karta hai, warna b — yeh hamesha ek value produce karta hai.
Output expr ek expression kyun hona chahiye, statement nahi?
Kyunki iski produced value wahi hai jo nayi list mein append hoti hai; ek statement store karne ke liye koi value produce nahi karta.
Kya tum if guard omit kar sakte ho?
Haan — [expr for x in iterable] har item rakhta hai.
Ternary a if cond else b kahan fit hota hai, aur kyun?
expr slot mein (before for), kyunki yeh ek expression hai jo ek value yield karta hai.
3**2 kya hai?
9 — 3 ko power 2 tak raise kiya.

Connections

  • For loops — woh engine jiske upar yeh poora topic sugar hai.
  • range() and iterablesfor x in iterable slot ko kya feed karta hai.
  • Conditional expressions (ternary) — expression-form if/else jo expr ke andar use hoti hai.
  • map() and filter() — transform (§7) aur filter (§5) ke functional equivalents.
  • Dictionary and set comprehensions — same foundations, alag brackets.
  • Generator expressions — lazy cousin; range ke unlike, truly one-shot (§2).
  • [[List comprehensions — `[expr for x in iterable if condition]` (index 1.2.34)|↑ Back to the parent topic]]