List Comprehension

List comprehension is a concise and powerful way to create new lists from existing iterables. It allows you to write clean, readable, and efficient code in a single line.
List comprehensions are widely used in data processing, filtering, and transformations.

What Is List Comprehension?

List comprehension provides a shorter syntax for creating a list using a loop.

Basic Syntax

python
[expression for item in iterable]
Example:
python
numbers = [1, 2, 3, 4]
squares = [x * x for x in numbers]
print(squares)

List Comprehension vs Traditional Loop

Using for Loop

python
numbers = [1, 2, 3, 4]
squares = []

for x in numbers:
    squares.append(x * x)

print(squares)

Using List Comprehension

python
numbers = [1, 2, 3, 4]
squares = [x * x for x in numbers]
print(squares)

List Comprehension with Condition

You can filter items using a condition.

Syntax

python
[expression for item in iterable if condition]
python
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = [x for x in numbers if x % 2 == 0]
print(even_numbers)
python
odd_numbers = [x for x in numbers if x % 2 != 0]
print(odd_numbers)

List Comprehension with if-else

You can use conditional expressions.

Syntax

python
[expression_if_true if condition else expression_if_false for item in iterable]
python
numbers = [1, 2, 3, 4]
result = ["even" if x % 2 == 0 else "odd" for x in numbers]
print(result)
python
values = [10, 5, 20]
status = [x if x > 10 else 0 for x in values]
print(status)

Nested List Comprehension

Used for working with nested lists.
python
matrix = [[1, 2], [3, 4], [5, 6]]
flattened = [num for row in matrix for num in row]
print(flattened)
python
pairs = [(i, j) for i in range(3) for j in range(2)]
print(pairs)

List Comprehension with Strings

python
text = "python"
letters = [char.upper() for char in text]
print(letters)
python
vowels = [char for char in text if char in "aeiou"]
print(vowels)

List Comprehension with Functions

python
def square(x):
    return x * x

numbers = [1, 2, 3]
result = [square(x) for x in numbers]
print(result)

List Comprehension with range()

python
numbers = [x for x in range(5)]
print(numbers)
python
squares = [x * x for x in range(1, 6)]
print(squares)

Modify Existing List Using Comprehension

python
numbers = [1, 2, 3, 4]
numbers = [x * 10 for x in numbers]
print(numbers)

Using List Comprehension with Conditions Only

python
data = ["Python", "", "Java", None]
cleaned = [x for x in data if x]
print(cleaned)

Common Mistakes

Wrong Order of if

python
# Incorrect
# [x for x in numbers if x % 2 == 0 else 0]
Correct:
python
[x if x % 2 == 0 else 0 for x in numbers]

Overusing List Comprehensions

Avoid using them when logic becomes complex.
python
# Hard to read
[x for x in numbers if x > 0 if x < 10]

Performance Insight

  • List comprehensions are generally faster than loops
  • They use less memory overhead
  • Best for simple transformations

Summary

  • List comprehension creates lists concisely
  • Syntax is compact and readable
  • Supports conditions and nested loops
  • Useful for filtering and transformations
  • Should be used wisely for clarity