Computer Science
Grade 9
20 min
Generators: Memory-Efficient Iteration
Understand generators and the `yield` keyword for creating iterators that save memory.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define what a generator is and explain how it differs from a standard list.
Create a simple generator function using the `yield` keyword.
Create a generator expression as a concise alternative to a generator function.
Iterate over a generator to retrieve its values one by one.
Articulate the primary benefit of using generators: memory efficiency.
Identify scenarios where a generator is more appropriate than a list.
Ever tried to open a massive 10-gigabyte file and watched your computer freeze? 🥶 What if you could read it one piece at a time instead of loading it all at once?
In this lesson, you'll learn about a powerful Python feature called generators. Generators let you work with huge sequences of data without using up all your computer's me...
2
Key Concepts & Vocabulary
TermDefinitionExample
IterationThe process of repeating a set of instructions or going through a sequence of items, one by one. A `for` loop is a common way to perform iteration.`for student in ['Alice', 'Bob', 'Charlie']:
print(student)`
Lazy EvaluationAn evaluation strategy where an expression is not calculated until the result is actually needed. Generators use this to save memory and processing time.A generator for numbers from 1 to 1 billion doesn't create all billion numbers at once. It only creates the number `1` when you ask for it, then `2` when you ask for the next one, and so on.
Generator FunctionA special kind of function that returns a generator object. It looks like a normal function but uses the `yield` keyword to produce a sequence o...
3
Core Syntax & Patterns
Generator Function Syntax
def my_generator_function(params):
# setup code
for item in some_sequence:
# logic to compute a value
yield value
Use this pattern to create a generator when the logic for producing values is complex. The function must contain at least one `yield` statement. Calling this function does not run the code; it returns a generator object.
Generator Expression Syntax
(expression for item in iterable if condition)
Use this pattern for creating simple, one-line generators. It's more memory-efficient and often more readable than creating a full list in memory, especially for large iterables. Note the use of parentheses `()`.
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
Following the pattern of the `even_numbers` generator in the tutorial, which function correctly generates the first `n` powers of 2 (starting with 2^0 = 1)?
A.def powers_of_two(n):
result = []
for i in range(n):
result.append(2**i)
return result
B.def powers_of_two(n):
for i in range(n):
return 2**i
C.def powers_of_two(n):
count = 0
result = 1
while count < n:
yield result
result *= 2
count += 1
D.def powers_of_two(n):
count = 0
while count < n:
count += 1
yield 2**count
Challenging
You need to find the first line containing 'CRITICAL_FAILURE' in a 50-gigabyte log file that is too large to fit in memory. Which approach is the most memory-efficient?
A.log_lines = open('log.txt').readlines()
for line in log_lines:
if 'CRITICAL_FAILURE' in line: print(line); break
B.with open('log.txt') as f:
for line in f:
if 'CRITICAL_FAILURE' in line:
print(line)
break
C.log_content = open('log.txt').read()
# search through log_content
D.log_lines = tuple(open('log.txt'))
# search through log_lines
Challenging
What is the output of the following code, which uses the `yield from` expression?
A.[1, <generator object letters at ...>, 2]
B.[1, ['A', 'B'], 2]
C.[1, 2, 'A', 'B']
D.[1, 'A', 'B', 2]
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free