List Comprehension0%
List Comprehension
Beginner9 min read•Updated: 2026-09-03
List Comprehension
List comprehensions provide a clean, one-line syntax to transform and filter iterables.
Key Concepts & Detailed Explanation
Syntax: [expression for item in iterable if condition] Benefits:
- 1Replaces 4-5 lines of for-loop with append() into a single readable expression.
- 2Implemented in optimized C under the hood, making comprehensions noticeably faster than standard append loops.
Code Examples & Output
Python
# Traditional loop vs List Comprehension
# Get squares of even numbers from 1 to 10
squares = [n ** 2 for n in range(1, 11) if n % 2 == 0]
print("Even squares:", squares)
# String transformation
names = ["sumit", "amit", "rohit"]
capitalized = [name.capitalize() for name in names]
print("Capitalized:", capitalized)
Expected Output:
Output
Even squares: [4, 16, 36, 64, 100]
Capitalized: ['Sumit', 'Amit', 'Rohit']
Best Practices & Common Pitfalls
If a comprehension spans more than 2 lines or has complex nested loops, revert to a standard for loop for clarity.
Practice Quiz
1. What is the result of '[x * 2 for x in [1, 2, 3]]'?
- A) [2, 4, 6]
- B) [1, 2, 3, 1, 2, 3]
- C) [2, 2, 2]
- D) [6]
Answer: A
Explanation: Each element is multiplied by 2, returning [2, 4, 6].
2. Can a list comprehension include an 'if' filtering clause?
- A) Yes, at the end: [x for x in seq if cond]
- B) No, only mapping is allowed
- C) Only with lambda
- D) Only in Python 2
Answer: A
Explanation: List comprehensions support optional trailing 'if' filters.
Practice Challenge
Create a list comprehension that extracts all words containing the letter 'e' from a sentence.
Next Lesson
Set Comprehension
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Iterating with Loops | Set Comprehension |
Practice Quiz
Test your understanding of this lesson with 1 questions. Each question has one correct answer.