Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
Blog · Python · 04 Jul 2026 · Hindi + English

Python में List Comprehension: Syntax, 8 Examples और फायदे

List comprehension एक line में list बनाती है: [expression for item in iterable if condition]. Basic से nested तक 8 examples if-else के साथ.

What is list comprehension?

List comprehension is a one-line way to build a new list from an existing iterable:
new_list = [expression  for item in iterable  if condition]
The if condition part is optional.
# Traditional way (4 lines)
squares = []
for n in range(1, 6):
    squares.append(n * n)

# List comprehension (1 line)
squares = [n * n for n in range(1, 6)]
print(squares)
[1, 4, 9, 16, 25]

8 examples from basic to advanced

# 1. Double every number
[n * 2 for n in [1, 2, 3]]              # [2, 4, 6]

# 2. Filter: only even numbers
[n for n in range(10) if n % 2 == 0]    # [0, 2, 4, 6, 8]

# 3. Filter + transform: squares of odd numbers
[n*n for n in range(10) if n % 2 == 1]  # [1, 9, 25, 49, 81]

# 4. Strings: uppercase names
[name.upper() for name in ["aman", "priya"]]   # ['AMAN', 'PRIYA']

# 5. if-else inside (note different position!)
["Pass" if m >= 33 else "Fail" for m in [25, 60, 90]]
                                        # ['Fail', 'Pass', 'Pass']

# 6. From a string: extract digits
[ch for ch in "a1b2c3" if ch.isdigit()] # ['1', '2', '3']

# 7. Nested loops: all pairs
[(x, y) for x in [1, 2] for y in ["a", "b"]]
                          # [(1,'a'), (1,'b'), (2,'a'), (2,'b')]

# 8. Flatten a 2D list
matrix = [[1, 2], [3, 4], [5, 6]]
[num for row in matrix for num in row]  # [1, 2, 3, 4, 5, 6]

The if vs if-else position rule (interview trap)

FormPositionExample
Only if (filter)AFTER the for[n for n in nums if n > 0]
if-else (choose value)BEFORE the for[n if n > 0 else 0 for n in nums]
Writing [n for n in nums if n > 0 else 0] is a SyntaxError — the else form must come before for. This exact trap is asked in written tests.

Why use it? (3 benefits)

  • Shorter: 4 lines become 1 readable line.
  • Faster: comprehension runs faster than an equivalent for+append loop because the append lookup is avoided internally.
  • Pythonic: it is the expected style in interviews and code reviews for simple transformations.

Limit: if the logic needs more than one if and one transform, a normal loop is more readable — do mention this balance in interviews.

List comprehension क्या है?

List comprehension existing iterable से नई list बनाने का one-line तरीका है:
new_list = [expression  for item in iterable  if condition]
if condition हिस्सा optional है.
# Traditional तरीका (4 lines)
squares = []
for n in range(1, 6):
    squares.append(n * n)

# List comprehension (1 line)
squares = [n * n for n in range(1, 6)]
print(squares)
[1, 4, 9, 16, 25]

Basic से advanced तक 8 examples

# 1. हर number double करें
[n * 2 for n in [1, 2, 3]]              # [2, 4, 6]

# 2. Filter: सिर्फ even numbers
[n for n in range(10) if n % 2 == 0]    # [0, 2, 4, 6, 8]

# 3. Filter + transform: odd numbers के squares
[n*n for n in range(10) if n % 2 == 1]  # [1, 9, 25, 49, 81]

# 4. Strings: names uppercase करें
[name.upper() for name in ["aman", "priya"]]   # ['AMAN', 'PRIYA']

# 5. अंदर if-else (position अलग है!)
["Pass" if m >= 33 else "Fail" for m in [25, 60, 90]]
                                        # ['Fail', 'Pass', 'Pass']

# 6. String से: digits निकालें
[ch for ch in "a1b2c3" if ch.isdigit()] # ['1', '2', '3']

# 7. Nested loops: सभी pairs
[(x, y) for x in [1, 2] for y in ["a", "b"]]
                          # [(1,'a'), (1,'b'), (2,'a'), (2,'b')]

# 8. 2D list flatten करें
matrix = [[1, 2], [3, 4], [5, 6]]
[num for row in matrix for num in row]  # [1, 2, 3, 4, 5, 6]

if vs if-else position rule (interview trap)

FormPositionExample
सिर्फ if (filter)for के BAAD[n for n in nums if n > 0]
if-else (value चुनें)for से PEHLE[n if n > 0 else 0 for n in nums]
[n for n in nums if n > 0 else 0] लिखना SyntaxError है — else वाला form for से पहले आना चाहिए. यही trap written tests में पूछा जाता है.

क्यों use करें? (3 फायदे)

  • छोटा: 4 lines एक readable line बन जाती हैं.
  • तेज़: comprehension बराबर वाले for+append loop से तेज़ चलती है क्योंकि append lookup internally avoid होता है.
  • Pythonic: simple transformations के लिए interviews और code reviews में यही style expected है.

Limit: अगर logic में एक से ज़्यादा if और transform चाहिए, तो normal loop ज़्यादा readable है — interviews में यह balance ज़रूर बताएं.

Frequently Asked Questions

Python में list comprehension क्या है?

List comprehension एक one-line syntax है [expression for item in iterable if condition] जो existing iterable से नई list बनाती है.

List comprehension में if-else कहां आता है?

Filter करने वाला if for के बाद आता है; values चुनने वाला if-else for से पहले आता है, जैसे [x if x>0 else 0 for x in nums].

क्या list comprehension for loop से तेज़ है?

हां, simple transformations के लिए यह for+append से measurably तेज़ है, क्योंकि method lookup और call overhead internally avoid होता है.

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें