What is yield used for in python?
Python Generators and the yield Keyword
Introduction
Generators are a special type of iterable in Python that allow you to iterate over data without storing the entire dataset in memory at once. This makes them particularly useful for working with large datasets or streams of data where memory efficiency is crucial.
What are Generators?
A generator in Python is a function that uses the yield keyword to return a value. Unlike a regular function that returns a single value and terminates, a generator can yield multiple values, one at a time, and maintain its state between each yield. When a generator function is called, it returns a generator object without even beginning execution of the function. When next() is called on the generator object, the function starts executing until it hits the yield statement, which returns the yielded value and pauses the function's execution. The next time next() is called, the function resumes right after the last yield statement.
The yield Keyword
The yield keyword is used to produce a value from a generator function and pause its execution. When the function is resumed, it continues execution immediately after the yield statement.
Example: Simple Generator
Here's a simple example of a generator that yields numbers from 1 to 3:
def simple_generator():
yield 1
yield 2
yield 3
# Create a generator object
gen = simple_generator()
# Iterate over the generator
for value in gen:
print(value)
Output:
1
2
3
Example: Generator for Memory-Efficient Iteration
Consider a scenario where you need to process a large file line by line. Reading the entire file into memory at once might not be feasible. Instead, you can use a generator to read the file line by line.
def read_large_file(file_path):
with open(file_path, 'r') as file:
for line in file:
yield line
# Create a generator object
file_gen = read_large_file('large_file.txt')
# Process each line in the file
for line in file_gen:
print(line.strip())
In this example, the read_large_file generator reads the file one line at a time, yielding each line to the caller. This approach is memory-efficient because it doesn't load the entire file into memory.
Example: Infinite Generator
Generators can also be used to create infinite sequences. For example, a generator that yields Fibonacci numbers indefinitely:
def fibonacci():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
# Create a generator object
fib_gen = fibonacci()
# Get the first 10 Fibonacci numbers
for _ in range(10):
print(next(fib_gen))
Output:
0
1
1
2
3
5
8
13
21
34
Benefits of Using Generators
- Memory Efficiency: Generators yield items one at a time and only when requested, which can significantly reduce memory usage.
- Lazy Evaluation: Generators compute values on the fly and only when needed, which can lead to performance improvements.
- Composability: Generators can be easily composed and chained together to build complex pipelines of data processing.
Conclusion
Generators and the yield keyword in Python provide a powerful way to create iterators that are both memory-efficient and elegantly simple. They are particularly useful for working with large datasets and streams of data where loading everything into memory is not practical.

