How to Use List Comprehensions and Generators to Transform Data in Python
Transform a list of integers by squaring even numbers with a list comprehension and cubing odd numbers with a generator.
Python code
19 linesdef transform_data(data):
"""
Transform a list of integers:
- squares of even numbers using a list comprehension
- cubes of odd numbers using a generator
"""
squares = [num ** 2 for num in data if num % 2 == 0]
cubes = (num ** 3 for num in data if num % 2 != 0)
return squares, cubes
if __name__ == "__main__":
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_squares, odd_cubes = transform_data(numbers)
print("Original data:", numbers)
print("Squares of even numbers:", even_squares)
print("Cubes of odd numbers:", list(odd_cubes))
Output
Original data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Squares of even numbers: [4, 16, 36, 64, 100]
Cubes of odd numbers: [1, 27, 125, 343, 729]
How it works
The transform_data function uses a list comprehension to build even_squares, filtering even numbers with the if clause and squaring each. It also creates a generator expression for odd_cubes, which lazily computes cube values for odd numbers. The generator is consumed once when converted to a list with list(odd_cubes) inside the print statement. Since generators are lazy, memory usage stays low for large datasets because values are produced one at a time only when needed.
Common mistakes
- Forgetting to convert the generator to a list before printing, which shows a generator object address instead of values
- Using the generator twice after it's been consumed, resulting in an empty second iteration
- Applying the `num ** 2` operation to odd numbers too, instead of filtering with `if num % 2 == 0`
Variations
- Use a tuple comprehension `tuple(num ** 3 for num in data if num % 2 != 0)` for eager evaluation
- Replace the `if` filter with `if num % 2` for odd numbers, which is shorter and equally correct
Real-world use cases
- Transforming sensor readings by filtering valid values and applying normalizers before analysis.
- Building feature vectors for ML pipelines by squaring or cubing selected numeric features.
- Processing paginated API responses where you map fields lazily without loading all items into memory.
Sponsored
More from Comprehensions & generators
- Batch Rows in Chunks with a Generator in Python easy
- Build a Generator Pipeline in Python: Filter Then Map medium
- Build a lazy generator to read file lines in Python easy
- Chunk an Iterable into Batches with a Generator in Python easy
- Convert Data in Python with Comprehensions and Generators easy
- Count Data in Python with Comprehensions and Generators easy
Keep learning
Related tutorials and quizzes for this topic.