0Pricing
Python Academy · Lesson

Generator Expressions

Build lazy sequences with generator expressions for memory efficiency.

Generator Expressions is a free Python Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction

Generator expressions are lazy comprehensions — they produce values one at a time without building a list in memory.

Generator Expression Syntax

(expression for item in iterable) uses parentheses. It returns a generator object, NOT a list.
gen = (x**2 for x in range(5))
print(gen)  # generator object
print(next(gen))  # 0
print(next(gen))  # 1

Memory Efficiency

A generator holds only one value at a time. sum(x**2 for x in range(10**7)) uses O(1) memory vs a list.
total = sum(x**2 for x in range(100))
print(total)

Consuming a Generator

Generators are consumed once. After exhaustion, StopIteration is raised and there is nothing left.
gen = (x for x in range(3))
for val in gen:
    print(val)
# gen is now exhausted

Generator as Function Argument

If a generator is the sole argument, you can omit the extra parentheses: sum(x**2 for x in range(10)).
print(sum(x**2 for x in range(10)))
print(max(len(w) for w in ['cat', 'elephant', 'ant']))

Chaining Generators

You can chain generators lazily: evens = (x for x in range(100) if x%2==0); big = (x for x in evens if x>50).
evens = (x for x in range(100) if x % 2 == 0)
big_evens = (x for x in evens if x > 50)
print(list(big_evens))

Converting to List/Tuple

list(gen) or tuple(gen) materializes all values. Do this when you need random access or multiple passes.
gen = (x**2 for x in range(5))
print(list(gen))

Generator Expression vs List Comprehension

Use generator when: you only iterate once, or the dataset is large. Use list when: you need len(), indexing, or multiple passes.
import sys
lst = [x**2 for x in range(1000)]
gen = (x**2 for x in range(1000))
print(sys.getsizeof(lst), sys.getsizeof(gen))

Infinite Generators

itertools.count(0) is an infinite counter. Pair with islice to take only what you need.
import itertools
first10 = list(itertools.islice(itertools.count(0), 10))
print(first10)

any() and all() with Generators

any(x>5 for x in lst) short-circuits — stops at first True. all() stops at first False. Very efficient.
nums = [1, 3, 7, 2]
print(any(x > 5 for x in nums))
print(all(x > 0 for x in nums))

Pipelines

Chain transformations lazily: data → filter → transform → aggregate. Each step is a generator; nothing runs until needed.
data = range(1000)
filtered = (x for x in data if x % 7 == 0)
transformed = (x * 2 for x in filtered)
print(sum(transformed))

Quick Check

What is the key memory advantage of a generator expression over a list comprehension?

Recap

Generator expressions: (expr for x in it). Lazy evaluation — O(1) memory. Consumed once. Use with sum/any/all/max. Convert to list when you need multiple passes.

Keep Going

Excellent! Continue to the next lesson to deepen your skills.

Frequently asked questions

Is the “Generator Expressions” lesson free?

Yes — the full text of “Generator Expressions” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Generator Expressions”?

Build lazy sequences with generator expressions for memory efficiency. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Generator Expressions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. List Comprehensions
  2. Set and Dict Comprehensions
  3. Generator Expressions
  4. The yield Keyword
← Back to Python Academy