Generators in Python Generators are a powerful tool in Python that allows you to generate a sequence of values on-the-fly, without having to store them all in memory at once.
What are Generators?
Generators are special types of functions that use the yield
keyword to produce a series of values, rather than computing them all at once and returning them in a list, for example.
Why Use Generators?
Basic Syntax
def my_generator():
yield value1
yield value2
yield value3
Example Code
def infinite_sequence():
num = 0
while True:
yield num
num += 1
gen = infinite_sequence()
print(next(gen)) # prints 0
print(next(gen)) # prints 1
print(next(gen)) # prints 2
In this example, infinite_sequence
is a generator that produces an infinite sequence of numbers. The next()
function is used to retrieve the next value from the generator.
Real-World Applications
Here’s an example of using a generator to process a large dataset:
Example: Processing a Large CSV File
Suppose we have a large CSV file data.csv
containing millions of rows, and we want to process each row without loading the entire file into memory.
Without Generators
import csv
with open('data.csv', 'r') as file:
reader = csv.reader(file)
data = list(reader) # Load entire file into memory
for row in data:
# Process each row
print(row)
This approach can lead to memory issues for large files.
With Generators
import csv
def read_csv(file_path):
with open(file_path, 'r') as file:
reader = csv.reader(file)
for row in reader:
yield row
for row in read_csv('data.csv'):
# Process each row
print(row)
In this example, the read_csv
generator yields each row of the CSV file on-the-fly, without loading the entire file into memory.
Benefits
Real-World Applications
Best Practices
yield from
for Delegation: Use yield from
to delegate generation to another generator.