List Comprehension in Python: Syntax, 8 Examples and Benefits
List comprehension creates a list in one line: [expression for item in iterable if condition]. 8 examples from basic to nested with if-else.
What is list comprehension?
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)
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)
| Form | Position | Example |
|---|---|---|
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] |
[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 क्या है?
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)
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)
| Form | Position | Example |
|---|---|---|
सिर्फ 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
What is list comprehension in Python?
List comprehension is a one-line syntax [expression for item in iterable if condition] that builds a new list from an existing iterable.
Where does if-else go in a list comprehension?
A filtering if goes after the for; an if-else that chooses between values goes before the for, like [x if x>0 else 0 for x in nums].
Is list comprehension faster than a for loop?
Yes, for simple transformations it is measurably faster than for+append, because the method lookup and call overhead is avoided internally.