How To Yield Nothing Python

In this blog post, we will explore how to use the yield statement in Python to produce no items in a generator function. In other words, we will create a generator that returns an iterator with no elements.

Understanding Generators and the yield Statement

Before diving into the concept of yielding nothing, let’s have a brief overview of generators and the yield statement in Python.

A generator is a special type of iterator, and it’s created by defining a function with the yield statement. When a generator function is called, it returns a generator object without actually executing the function. The generator function can then be iterated using a for loop or the next() function. Upon each iteration, the function is executed until the yield statement is encountered, and then the yielded value is returned to the caller.

Yielding Nothing

To create a generator that yields no items, we simply have to define a generator function without any yield statement, like so:

def empty_generator():

Here, the empty_generator function contains no yield statement, and it will produce an empty iterator when called. Let’s see this in action:

eg = empty_generator()

for item in eg:

# No output

As you can see, nothing is printed when iterating over the generator object eg because there are no elements to yield.

Use Cases for an Empty Generator

While an empty generator might not seem very useful at first glance, it can be handy in a few situations:

  1. Placeholder: You might need a placeholder generator for a future implementation, and you don’t want it to produce any results yet. An empty generator can serve as a temporary stub for future development.
  2. Conditional Generation: In some cases, you might want to conditionally generate items based on input parameters or other factors. An empty generator can be used as the return value when no items should be generated.


In this post, we’ve learned how to create a generator that yields nothing in Python. By defining a generator function without any yield statement, we can create an iterator with no elements. While such an empty generator might not be useful in everyday scenarios, it can still be handy as a placeholder or for conditional generation purposes.