The following article describes How to Use Generators in Python.
Basically, Generators in Python are a way to create iterators, which are objects that can be iterated (looped) over, but they don’t store the entire sequence of values in memory. Instead, they generate values one at a time on-the-fly, which can be very memory-efficient for large data sets or infinite sequences.
As a matter of fact, you can create generators in Python using two main methods: generator functions and generator expressions.
Generator Functions
Generator functions are defined using the yield
keyword in a function. When a function contains a yield
statement, it becomes a generator function. For example.
def simple_generator():
yield 1
yield 2
yield 3
# Create a generator object
my_generator = simple_generator()
# Iterate over the generator
for value in my_generator:
print(value)
When you call simple_generator()
, it returns a generator object. So, the generator only generates one value at a time when you iterate over it. The state of the generator function is saved between each yield
statement.
Generator Expressions
Actually, Generator expressions are similar to list comprehensions but use parentheses instead of square brackets. They create a generator object, which you can iterate over. For example.
squares = (x**2 for x in range(1, 6))
# Iterate over the generator expression
for square in squares:
print(square)
In this example, squares
is a generator expression that generates the squares of numbers from 1 to 5. It’s efficient because it doesn’t create a list of squares in memory; it generates them as needed.
Key points to remember when using generators
- Generators are lazy and memory-efficient, making them suitable for large data sets or infinite sequences.
- Moreover, you can iterate over generators using
for
loops or by callingnext()
on the generator object. - Once a generator is exhausted (i.e., it has no more values to yield), it raises a
StopIteration
exception. - Also, you can create custom generator functions to yield values based on your requirements, including reading data from files, databases, or APIs one item at a time.
The following example shows a custom generator function that yields Fibonacci numbers.
def fibonacci_generator():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
# Create a generator object for Fibonacci numbers
fib_gen = fibonacci_generator()
# Generate and print the first 10 Fibonacci numbers
for _ in range(10):
print(next(fib_gen))
So, this generator will keep generating Fibonacci numbers indefinitely.
Further Reading
How to Perform Dataset Preprocessing in Python?
Spring Framework Practice Problems and Their Solutions
How to Implement Linear Regression from Scratch?
Getting Started with Data Analysis in Python
Wake Up to Better Performance with Hibernate
Data Science in Insurance: Better Decisions, Better Outcomes
Breaking the Mold: Innovative Ways for College Students to Improve Software Development Skills