Introduction
Python comprehensions provide a concise way to create lists, dictionaries, sets, and generators. They offer a more readable and often faster alternative to traditional loops. Comprehensions are widely used in data science for data transformation, filtering, and feature engineering tasks.
Key Concepts
- List comprehension: Create lists with inline expression
- Dictionary comprehension: Create dictionaries from iterables
- Set comprehension: Create sets with unique elements
- Generator expressions: Memory-efficient iteration
- Nested comprehensions: Multiple levels of iteration
- Conditional filtering: Using if in comprehensions
Python Implementation
# List comprehension - basic
squares = [x**2 for x in range(10)]
doubles = [x * 2 for x in range(5)]
# List comprehension with condition
evens = [x for x in range(20) if x % 2 == 0]
odd_squares = [x**2 for x in range(10) if x % 2 != 0]
# Dictionary comprehension
word_lengths = {word: len(word) for word in ["hello", "world", "python"]}
squares_dict = {x: x**2 for x in range(5)}
# Set comprehension
unique_lengths = {len(word) for word in ["apple", "banana", "cherry", "date"]}
# Nested comprehension - flatten matrix
matrix = [[1, 2], [3, 4], [5, 6]]
flat = [num for row in matrix for num in row] # [1,2,3,4,5,6]
# Nested list comprehension - create matrix
transposed = [[row[i] for row in matrix] for i in range(2)]
# Generator expression (memory efficient)
sum_squares = sum(x**2 for x in range(1000000))
max_value = max(x for x in data if x > 0)
# Dictionary with condition
ages = {"Alice": 25, "Bob": 30, "Charlie": 35, "Diana": 22}
adults = {name: age for name, age in ages.items() if age >= 25}
When to Use
- Transforming data in pandas operations
- Creating features from raw data
- Filtering datasets based on conditions
- Building lookup dictionaries
- Flattening nested data structures
- Memory-efficient processing of large datasets
Key Takeaways
- Comprehensions are generally faster than equivalent for loops in Python
- Generator expressions save memory when working with large datasets
- Keep comprehensions simple; complex logic may be clearer in regular loops
- Dictionary comprehensions are ideal for creating lookup tables and mappings
- Avoid nesting more than two levels for readability