Back to Course
Python Essentials: The Engineering Approach
Module 14 of 20
14. Iterators & Generators
Lazy Evaluation
What if you need to process 1,000,000,000 rows? A list will crash your RAM. A Generator produces one item at a time.
The yield keyword
Functions that return die. Functions that yield pause.
pythondef infinite_numbers(): n = 0 while True: yield n n += 1 gen = infinite_numbers() print(next(gen)) # 0 print(next(gen)) # 1