Learn with Yasir

Share Your Feedback

Python List Comprehension: Syntax, Examples & Real-World Uses


Learn Python list comprehension with clear syntax, beginner-friendly examples, and real-world applications. Discover how to write concise, efficient code for data processing, filtering, and transformations.

What is List Comprehension?

List comprehension is a concise way to create lists in Python. It provides a compact syntax for transforming or filtering data from one list (or any iterable) to another. Instead of using a traditional for loop with .append(), you can often express list operations more elegantly with list comprehension.

Why Use List Comprehension?

  1. Readability: Once you’re familiar with the syntax, list comprehensions are often more readable than equivalent loop constructs.
  2. Conciseness: They allow you to express complex operations in a single line.
  3. Performance: List comprehensions can be slightly faster than equivalent for loops in many cases.
  4. Pythonic: They are considered a more “Pythonic” way to create lists.

Basic Syntax

[expression for item in iterable if condition]
  • expression: What you want to do with each item
  • item: The variable representing each element in the iterable
  • iterable: The sequence you’re looping through (list, tuple, string, etc.)
  • condition (optional): Filters which items to include

Examples for Beginners

Example 1: Basic transformation

# Traditional way
numbers = [1, 2, 3, 4]
squares = []
for num in numbers:
    squares.append(num ** 2)

# With list comprehension
squares = [num ** 2 for num in numbers]
# Result: [1, 4, 9, 16]

Example 2: Filtering

# Only even numbers
numbers = [1, 2, 3, 4, 5, 6]
evens = [num for num in numbers if num % 2 == 0]
# Result: [2, 4, 6]

Example 3: Combining transformation and filtering

# Squares of even numbers only
numbers = [1, 2, 3, 4, 5, 6]
even_squares = [num ** 2 for num in numbers if num % 2 == 0]
# Result: [4, 16, 36]

Real-Life Usage Examples

1. Processing user input

# Convert comma-separated string to list of integers
user_input = "1, 2, 3, 4, five, 6"
numbers = [int(x.strip()) for x in user_input.split(',') if x.strip().isdigit()]
# Result: [1, 2, 3, 4, 6]

2. Data cleaning

# Remove empty strings and strip whitespace
data = [" apple ", "banana", "  ", "cherry ", ""]
clean_data = [fruit.strip() for fruit in data if fruit.strip()]
# Result: ["apple", "banana", "cherry"]

3. Working with files

# Read lines from a file and remove newline characters
with open('data.txt') as file:
    lines = [line.strip() for line in file]

4. Matrix operations

# Transpose a matrix (swap rows and columns)
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
transposed = [[row[i] for row in matrix] for i in range(3)]
# Result: [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

5. API response processing

# Extract specific fields from API response
api_response = [
    {"id": 1, "name": "Alice", "active": True},
    {"id": 2, "name": "Bob", "active": False},
    {"id": 3, "name": "Charlie", "active": True}
]

active_users = [user["name"] for user in api_response if user["active"]]
# Result: ["Alice", "Charlie"]

When Not to Use List Comprehension

While list comprehensions are powerful, they’re not always the best choice:

  • When the logic is too complex (becomes hard to read)
  • When you need to use multiple conditions that would make the comprehension too long
  • When you need to include side effects (like printing) during iteration

In these cases, a traditional for loop might be more appropriate.

List comprehension examples: https://youtube.com/shorts/cnSLqqx_huQ?si=5J19IZzT23VvtDmp