The yield keyword converts a normal function to a generator type of function. Used in generator functions to return the values in sequence when requested for values using the next() function. The yield statement returns a generator object to the one who calls the function that contains yield instead of simply returning a value. 

def gen_func(x):
    for i in range(x):
        yield i

Functions benefit from the yield keyword during generator operations to provision multiple value sequences into sequence through function execution. The function cannot erase the original values stored in local variables. The execution begins at the yield expression when any function is invoked. Generator functions are functions that include the yield keyword in their code. 

Generator function creation requires the use of yield keyword syntax. The generator function operates with an iterator structure that maintains memory efficiency while functioning like object iterators. The yield keyword allows the function execution to continue until reaching the end, unlike the return keyword, which terminates function processing.

What Is Yield In Python?

What Is Yield In Python?

Yield operates as a Python feature in generator functions to provide sequential value retrieval through lazy evaluation methods. The function pauses at yield statements while preserving its current state and then pauses before each subsequent value return. The function pauses at its previous yield statement before returning each successive value that gets requested. 

  • Generates a Value: Each time the generator function is called, it "yields" a value, and the function's state is paused until the next value is requested.
  • The state is Preserved: Unlike regular functions that start from the beginning each time they are called, a generator function remembers where it left off. This means it can continue where it stopped and yield the next value.
  • Lazy Evaluation: Values are generated only when they are requested (iterated over). This is beneficial for working with large sequences of data where you don’t want to store everything in memory at once.

Example

def simple_generator():
    yield 1
    yield 2
    yield 3

gen = simple_generator()
for value in gen:
    print(value)

Output:

1
2
3

What Does the Yield Keyword Do?

What Does the Yield Keyword Do?

The yield keyword is used to make a function generator. It allows to return of a value in sequence. Instead of computing all values at once, it returns in sequence.

Yield is a function that generates values one at a time when requested, saves its state, and acts as an iterable behavior, allowing iteration over variables and code positions. Produces values lazily: The function does not return the final result immediately.

Here’s how yield works:

  • Produces values lazily: Instead of giving the final result it generates one time when requested.
  • The state is saved: When the yield function is hit, it pauses and remembers the position in code also variable values.
  • Iterable behavior:  When the function uses the yield keyword, it acts like a generator. They generate value in sequence.

Example

def count_up_to(n):
    count = 1
    while count <= n:
        yield count  # Yield the current value of the count
        count += 1  # Increment count

# Using the generator function
counter = count_up_to(3)
for number in counter:
    print(number)

Output:

1
2
3

Difference Between Return and Yield Python

Python uses both the yield and return send values from a function. A key difference in both is the use cases we see in the following table.

Aspectreturnyield
Purpose
The function returns value contents while completing its execution.
This statement first pauses the function and then produces sequential value outputs.
Function TypeThe standard function stops after returning values.The generator function generates values step by step through sequential output.
Return ValueThe values will return before the function terminates. The operation returns a value for future function continuation.
Memory UsageReturns all values at once (e.g., in a list).The program requires small amounts of memory while it generates values sequentially.
State RetentionA function state loses its value when the program returns to it.Returns the function state for later continuation after each yield execution.
IterationThe statement cannot be traversed because it requires either an enumerated list or a collection.The feature enables it to work within loops.
End of FunctionThe function terminates after return.The function operates until Python encounters StopIteration.

Generator Functions In Python

A generator function helps developers to produce a sequence of values via yield statements instead of executing entire values simultaneously. The generator function achieves exceptional results for processing extensive data volumes.

  • A generator function requires at least one yield statement to operate. A value output by yield functions enables them to place execution on hold before resuming later from the same point.
  • The generator function returns a Generator object, which prevents immediate execution whenever it is triggered. The function delivers a generator object, which allows lazily obtaining values during iteration.
  • The generator function maintains its current execution state while preserving both local variable values and the current processing position when it becomes inactive during calls. The function keeps running from the point where it last produced a result.
  • The system uses fewer memory resources to store data sequences because generators operate without full-value production at once.

Example

def countdown(n):
    while n > 0:
        yield n  # Yield the current value of n
        n -= 1  # Decrease n by 1
    print("Done!")

# Create a generator object
counter = countdown(5)

# Iterate through the generator
for num in counter:
    print(num)

Output:

5
4
3
2
1
Done!

Example 1: Generator Functions and Yield Keyword in Python

How do yield keywords work?

The following example demonstrates the utility of generator functions together with the yield keyword.

Problem:

A generator function needs creation to produce the first n square numbers via the yield protocol.

Solution:

def generate_squares(n):
    for i in range(1, n + 1):
        yield i * i  # Yield the square of each number from 1 to n

# Create a generator object
squares_generator = generate_squares(5)

# Iterate through the generator to print the squares
for square in squares_generator:
    print(square)

Output:

1
4
9
16
25

Example 2: Generating an Infinite Sequence using yield

In Python, you can use a generator function to create an infinite sequence of values. Since generators yield values one by one and do not store the entire sequence in memory, they are perfect for creating infinite sequences. Let's create an example that generates an infinite sequence of natural numbers starting from 1.

Solution:

def infinite_counter():
    count = 1
    while True:
        yield count  # Yield the current count, then increment
        count += 1    # Increment the counter

# Create a generator object
counter = infinite_counter()

# Print the first 10 numbers from the infinite sequence
for i in range(10):
    print(next(counter))

Output:

1
2
3
4
5
6
7
8
9
10

Example 3: Demonstrating yield Working with a List

In this example, let's use the yield keyword to generate a filtered list of even numbers from a list of integers.

Problem:

Given a list of numbers, we want to use a generator function to yield only the even numbers from that list one by one.

Solution:

def even_numbers(numbers):
    for num in numbers:
        if num % 2 == 0:  # Check if the number is even
            yield num  # Yield the even number

# List of numbers
numbers_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Create a generator object
even_gen = even_numbers(numbers_list)

# Iterate through the generator to print even numbers
for even in even_gen:
    print(even)

Output:

2
4
6
8
10

Example 4: Use of yield Keyword with Boolean Values

In this example, we’ll use the yield keyword in a generator function to generate Boolean values based on some condition, such as checking if numbers in a range are divisible by a given number.

Problem:

Create a generator function that checks if numbers in a given range are divisible by a specified divisor and yields True or False accordingly.

Solution:

def is_divisible_by(numbers, divisor):
    for num in numbers:
        if num % divisor == 0:
            yield True  # Yield True if the number is divisible by the divisor
        else:
            yield False  # Yield False if the number is not divisible by the divisor

# List of numbers to check
numbers_list = [10, 15, 20, 25, 30, 35, 40]

# Divisor
divisor = 5

# Create a generator object
divisibility_gen = is_divisible_by(numbers_list, divisor)

# Iterate through the generator and print the Boolean results
for result in divisibility_gen:
    print(result)

Output:

True
True
True
False
True
False
True

How Can You Call Functions Using Yield?

How Can You Call Functions Using Yield?

Step 1: Define a Generator Function

A generator function creates lazy value sequences using the yield keyword while preserving its state and execution point until a request for the next value appears. A defined generator function operates in a manner distinct from traditional functions because of its yield keyword.

  • The function pauses as it encounters yield and then provides a value immediately from its current position.
  • After the function encounters yield to pause its execution it maintains its state to resume from where it stopped.
  • The generator operation continues until the generator depletes all values or terminates by design.

Step 2: Call the Generator Function

A generator function remains inactive during its initial execution call. The generator function produces an iterator object, also known as a generator object, which functions as an object that can generate values upon request. The function remains stopped at this stage before starting its execution.

  • The generator function execution creates a generator object, but the value generation happens only when the system requests particular values.

Step 3: Iterate Over the Generator

The generator values need to be retrieved by moving through the generator object. There exist two methods to execute this operation.

  • The for loop generates automatic calls to the generator that deal with iteration responsibilities. The loop receives its next value from the generator, which yields this value at each loop execution. The loop terminates itself after the generator runs out of yieldable values.
  • You can extract the next generator output by calling the next() function. Next() function calls activate the generator at its prior suspension position until it meets the subsequent yield statement.

The generator function stops running whenever it reaches the yield instruction in both situations. The function stops its execution to let the caller receive the yielded value. After its suspension by a call from either a loop or next(), the generator function continues execution from the position it stopped at the last yield.

Step 4: Resume Execution with Each Yield

A generator function allows resuming execution by using the syntax Yield every time a new value is requested.

  • A generator pauses its execution when it meets a yield statement, allowing it to resume operation every time the program requests another value. Generator functions delay their complete code execution until they generate individual output values for efficient memory usage and lazy evaluation strategy.
  • The balance between generator execution and caller flow is maintained by each value output because the generator passes control to its origin. The function maintains the generator's internal state each time it yields so it can resume execution at the exact point where it last yielded by returning to its previous internal state (local variables, loop counter,s, etc.).

Through the completion of its value generation process, the generator will execute:

An exception known as StopIteration alerts the program that the end of iteration has arrived since no additional values await generation. A generator ends its operation after it exhausts all yield statements in the definition.

The ability of generators to operate lazily provides significant advantages for working with enormous datasets or continuous streams of data, which avoids complete memory usage. The value generation process progresses in a one-at-a-time manner.

Why And When Should You Use Yield?

Use yield is good in some scenarios. Let's see when and why to use the yield keyword. 

Why Should You Use Yield?

Why Should You Use Yield?

  • Memory Efficiency: The yield keyword enables you to create values that are produced individually instead of taking up complete memory space. Yield provides valuable functionality for dealing with big datasets along with long sequences that cannot be efficiently stored as complete data or value sets. The use of yield in programming proves beneficial when processing extensive files line by line or creating extensive number sequences because it optimizes memory usage effectively.
  • Lazy Evaluation: The values created through yield become available only upon request instead of being created in advance. This is called lazy evaluation. Using yield lets you work only with data when needed because this method outperforms the upfront computing approach, especially when not every value is needed. When you run a sequence iteration that needs the first few values, only the generator computes those items before skipping the remaining ones.
  • Improved Performance for Infinite Sequences: Yield allows you to establish a process that produces continuous sequences of output. The demanded values are automatically generated by the function, meaning full sequence storage in memory becomes unnecessary. A Fibonacci sequence can be generated indefinitely without wasting excessive memory because the generator provides value generation indefinitely when you continue to request output.
  • Better Handling of Large Data Streams: Yield enables data processing on streams since it allows you to work incrementally with data sources, including file reading and API and network data retrieval. The method surpasses traditional processing methods because it avoids loading whole datasets into memory before execution.

When Should You Use Yield?

  • You should employ yield for large datasets that need individual element processing rather than complete collection processing. The processing of a massive CSV file needs yield because loading the whole file into memory would require insufficient resources. The yield statement enables you to return single lines instead of the whole list with one command.
  • Processing log files together with reading large data files through chunks constitutes an example use case.
  • The yield keyword serves as a fantastic solution to build endless value series (such as Fibonacci numbers or prime numbers) when working with infinite sequences. By not requiring complete sequence calculation in advance, your program can offer unlimited value generation when the request comes from the caller.

Advantages of Yield in Python

Advantages of Yield in Python

yield produces memory-efficient value generation that lets programmers manage big datasets along with infinite sequences while minimally using system memory.

The yield keyword both makes stateful iteration easier to implement and delivers better performance through postponed computational tasks. The code maintains both efficiency and cleanness because of yield functionality.

  • Memory Efficiency: The sequential values output prevents the requirement to store complete dataset information in memory.
  • Lazy Evaluation: The evaluation takes place only upon demand thus enhancing performance,e especially when working with large data collections and endless sequences.
  • Improved Performance: The incremental process of data handling yields better efficiency than executing computations beforehand.
  • Support for Infinite Sequences: The programming system creates a boundless series of values without using large memory resources.
  • Stateful Iteration: The generator function stores its current state during one processing cycle to later retrieve it before the next iteration thus enabling context maintenance.
  • Simpler Code: By using generators programmers achieve more readable code structures that address complex iteration needs compared to loop structures or manual iterators.

Disadvantages of Yield in Python

Disadvantages of Yield in Python

The debugging process may become more complex because the yield functionality pauses and resumes execution during computation.

Functions working with a complete dataset find yield incompatible, while new programmers face challenges in understanding its behavior. The process to access values through iteration might not suit every situation since it raises compatibility issues.

  • Difficult to Debug: Debugging generator programs becomes difficult because execution pauses while resuming operations hindering the following of program flow.
  • Limited Compatibility with Built-in Functions: A few Python functions demand indexable collections made up of lists or tuples. At the same time, generators inherently cannot provide this capability (for example, indexing and slicing are unavailable).
  • Execution Happens Only on Demand: Upfront access to the generator code data remains impossible because the code system executes based on need.
  • Requires Iteration to Access Values: Codes inside generators must be explicitly executed through iteration before you can access their contents since they do not operate like stand-alone collections.

Conclusion

The Yield keyword in Python enables efficient on-demand value production, which keeps memory utilization low and boosts system performance, mostly when working with extensive data collections or endless sequences. The yield keyword simplifies stateful iteration but produces cleaner code while it makes debugging harder and requires compatibility testing for various functions.

FAQ's

👇 Instructions

Copy and paste below code to page Head section

A Python generator function requires yield as its main keyword for creation. From within the function, execution pauses when yield produces values sequentially so the function can recover from the point where it is suspended when asked to return.

In comparison to the return's exit method, yield pauses the function execution for a value generation that lets the function continue from its last suspension point during value requests. This enables lazy evaluation.

Yield serves memory-efficient data handling of big datasets and endless sequences as well as data processing through incremental steps.

The definition of a generator function includes at least one yield syntax within its execution. Using yield allows the function to return a generator object with automatic lazy value generation upon loop iteration.

Generator functions hold the capacity to provide sequential values to their users. The function pauses when it encounters yield statements to return their values. A generator resumes its operation from the exact point of interruption when getting new value requests.

A generator works successfully within the structure of a for loop. A for loop carries out automatic next() function calls to the generator until it depletes all available values.

Ready to Master the Skills that Drive Your Career?
Avail your free 1:1 mentorship session.
Thank you! A career counselor will be in touch with you shortly.
Oops! Something went wrong while submitting the form.
Join Our Community and Get Benefits of
💥  Course offers
😎  Newsletters
⚡  Updates and future events
undefined
undefined
Ready to Master the Skills that Drive Your Career?
Avail your free 1:1 mentorship session.
Thank you! A career counselor will be in touch with
you shortly.
Oops! Something went wrong while submitting the form.
Get a 1:1 Mentorship call with our Career Advisor
Book free session
a purple circle with a white arrow pointing to the left
Request Callback
undefined
a phone icon with the letter c on it
We recieved your Response
Will we mail you in few days for more details
undefined
Oops! Something went wrong while submitting the form.
undefined
a green and white icon of a phone