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.

Easy Python 3.9+ Aug 9, 2026 Comprehensions & generators 15 views 0 copies

Python code

19 lines
Python 3.9+
def 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

stdout
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

  1. Use a tuple comprehension `tuple(num ** 3 for num in data if num % 2 != 0)` for eager evaluation
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Comprehensions & generators

Related tutorials and quizzes for this topic.