Python provides the capability to create your own iterator function using a construct known as a generator.
💡 A generator is a unique kind of function. Unlike traditional functions that return a single value, a generator returns a special object — an iterator, which can produce a sequence of values over time.
yield statement allows the function to produce values one at a time, pausing in-between, instead of computing them all at once. This can make your code more efficient and memory-friendly when dealing with large data sets.
When a generator function is called, it doesn’t actually run the code in the function. Instead, it returns a generator object. This object can then be iterated over (for example, in a
for loop or by using the
next() function) to execute the function and retrieve the values it yields, one by one, as needed.
You can return a generator from a function by using the
yield keyword. Here’s an example:
def fibonacci(): a, b = 0, 1 while True: yield a b, a = a + b, b
In this example, the
fibonacci function is a generator function because it uses the
yield keyword. Every time you call
next() on a generator created by this function, it will yield the next Fibonacci number.
(If you don’t understand the Fibonacci series yet, check out my fun blog tutorial:) 👇
💡 Recommended: Fibonacci in One Line Python
Here’s how you might use this generator:
gen = fibonacci() print(next(gen)) # Output: 0 print(next(gen)) # Output: 1 print(next(gen)) # Output: 1 print(next(gen)) # Output: 2 print(next(gen)) # Output: 3
next() is called, the function’s state is preserved, so it can remember the values of
b. The next time
next() is called, the function picks up where it left off and yields the next value.
👉 Note: Generator functions return a special type of iterator, which is the generator. When the generator’s
__next__() method (or the built-in function
next()) is called, the function runs until it hits a
yield statement, then it yields its value and pauses.
__next__() is called again, the function resumes running from where it left off, starting after the
yield statement, until it hits another
yield statement (or returns, which signals the generator is done).
💡 Recommended: Python Generator Expressions
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.