What does Yield keyword do in Python

Spread the love

Do you want to know what does Yield keyword do in Python? In this article, you will see a comprehensive explanation of the Yield keyword. The yield keyword is a feature in Python that allows a function to return a value and pause its execution, preserving its state for the next time it is called.

Quick Examples of Yield Keyword in Python

If you are in hurry, we will go over some quick examples of how the yield keyword can be used in Python.


# Example No 1
def my_func():
    yield 1
    yield 2
    yield 3

values = my_func()
print(next(values))  # Output: 1
print(next(values))  # Output: 2
print(next(values))  # Output: 3

# You can also Iterate using for loop
for value in my_func():
    print(value)

# Example No 2 : Finding Squares 
def square(numbers):
    for number in numbers:
        yield number**2

values = square([1, 2, 3])
print(next(values))  # Output: 1
print(next(values))  # Output: 4
print(next(values))  # Output: 9

# Example No 3 : Convert Lower to upper case
def convert_upper(string):
    for character in string:
        if character.isalpha():
            yield character.upper()

values = convert_upper("nnk")
print(next(values))  # Output: N
print(next(values))  # Output: N
print(next(values))  # Output: K

The next() function is used to retrieve the next value in the generator’s sequence. This process can be repeated until the generator is exhausted, at which point the StopIteration exception is raised.

1. Generating large sequences of values using yield keyword

This can be a very good use case where we want to generate a large sum of values based on some calculations. In this case, using yield keyword can be a useful way to save memory and improve performance when working with large sequences of data.


def generate_seq():
    for i in range(1, 11):
        yield i

for number in generate_seq():
    print(number)

# the output is number from 1 to 11

2. Using yield to Iterate Over Data Streams in Python

Let’s say you have a stream of data, a file stream, where you want to read the file line by line. One way is the traditional way of reading the file which is just to use the iterable concept. just read the file line by line in a loop.

The second and best way is to use generators. Now the question is, how can you create the generators? well, in python we can use the yield keyword to build a generator function. See the below example.

2.1 Example No 1: Using Yield keyword to read a file


def read_lines(filename):
    with open(filename, 'r') as f:
        for line in f:
            yield line

for line in read_lines('my_file.txt'):
    print(line)

Iterating over data streams with yield keyword can be a convenient and efficient way to process large amounts of data. Generators, which in python can be created using the yield keyword, allow you to iterate over the data one piece at a time, rather than reading the entire stream into memory at once. This can be especially useful when working with large files or datasets that would not fit in memory.

2.2 Using yield keyword to read records from database

We can use Generators to iterate over data from a database, such as records in a table.


def fetch_records(conn):
    cursor = conn.cursor()
    cursor.execute('SELECT * FROM my_table')
    for row in cursor:
        yield row

for row in fetch_records(conn):
    print(row)

In this example, the fetch_records generator function executes a database query and yields each row as it is fetched from the database. This allows you to process the rows of query one at a time. And rather than fetching the entire result set into memory at once.

2.3 Benefits of using yields keyword to read data from stream

Below are a few benefits of using the yield keyword. These are the cases where we want to read the data from a stream using the yield keyword.

  1. The generator function yields values one at a time, rather than reading the entire data stream into memory at once.
  2. The generator function executes only when the generator is iterated over, and reads or fetches data as needed.
  3. Generators can be iterated over using a for loop or other iteration constructs.
  4. Generators can generate data on-the-fly, rather than computing all data upfront.
  5. Generators allow you to process large amounts of data efficiently, without using up too much memory.

3. Using yield keyword to Implement Lazy Evaluation in Python

The yield keyword is an essential part of implementing lazy evaluation in Python. In a generator function, the yield keyword is used to yield a value to the caller, allowing the generator to generate a sequence of values one at a time. This is different from a regular function, which executes the function body and returns a single value.


def read_lines(filename):
    with open(filename, 'r') as f:
        for line in f:
            yield line

def process_lines(lines):
    for line in lines:
        # Process the line
        print(line)

# Read and process the lines of a file one at a time
for line in read_lines('my_file.txt'):
    process_lines(line)

4. Generating Data on-Demand with yield in Python

The yield keyword is an essential part of generating data on-demand with generators. When used in a generator function, the yield keyword allows you to generate a sequence of values one at a time, rather than computing all the values upfront. This can be especially useful when working with large datasets that may not fit in memory all at once.


def fibonacci(n):
    a, b = 0, 1
    for i in range(n):
        yield a
        a, b = b, a + b

for i in fibonacci(10):
    print(i)

5. Using the Yield Keyword to Implement Coroutines with Generators in Python

The yield keyword is an essential part of implementing coroutines with generators in Python. When used in a generator function, the yield keyword allows you to pause the execution of the coroutine and wait for a value to be sent to it.


def search_for_string(string):
    print(f"Searching for string: {string}")
    while True:
        line = (yield)
        if string in line:
            print(f"String found: {line}")

# Create the coroutine
searcher = search_for_string("sparkbyexamples")

# Start the coroutine
next(searcher)

# Send some lines to the coroutine
searcher.send("useful tutorials on sparkbyexamples.com!")
searcher.send("checked out examples on sparkbyexamples.com?")
searcher.send("data processing with sparkbyexamples.com.")
searcher.send("sparkbyexamples.com is my favorite.")

# Close the coroutine
searcher.close()

6. Creating a Data Pipeline with yield keyword in Python

The yield keyword is an essential part of creating data pipelines with generators in Python. By using the yield keyword in generator functions, you can pass data through a series of processing steps, one step at a time. This can be especially useful when working with large datasets. When you need them to transform it or filter it in some way.


def read_csv(filename):
    with open(filename, "r") as f:
        for line in f:
            yield line.strip().split(",")

def filter_columns(rows, indices):
    for row in rows:
        yield [row[i] for i in indices]

def convert_to_float(rows):
    for row in rows:
        yield [float(cell) for cell in row]

def compute_average(rows):
    total = 0
    count = 0
    for row in rows:
        total += sum(row)
        count += len(row)
    yield total / count

# Create the pipeline
filename = "data.csv"
indices = [1, 2, 3]
pipeline = compute_average(convert_to_float(filter_columns(read_csv(filename), indices)))

# Execute the pipeline
average = next(pipeline)
print(f"Average: {average:.2f}")

Summary and Conclusion

You have learned about the yield keyword in Python. You now know that the yield keyword is used to create generator functions and generator expressions, which generate a sequence of values over time. In this artile You have also understand that the yield keyword has many use cases. This includes generating large sequences of data and improving the performance of your code. Let me know if you have any questions

Leave a Reply

You are currently viewing What does Yield keyword do in Python