What Does the “Yield” Keyword Do?

Iterables, functions, and generators in Python

Jonathan Hsu
Nov 5 · 4 min read
Photo by C Drying on Unsplash

To learn about the yield keyword, there are a variety of other terms and concepts that you must be comfortable with first. Before we dive into all of that, let’s keep the end in mind and talk through why yield is beneficial.

Using yield will improve the memory efficiency — and subsequently, the speed/performance — when looping over a large iterable. These benefits will be less pronounced for small data sets; however, as data grows so too does the benefit of using yield.

Now, onto the explanation. In short, yield is a replacement for the return keyword that produces a generator from a function, which is used as a one-time iterable. If you’re like me when I first learned this, you got lost along the way. So, to unpack this concept, let’s start from the ground and work our way up.


What Is an Iterable?

for x in my_iterable:
pass

What’s important to note is that while individual elements are accessed one at a time, the entire iterable is still stored in memory. That means that if you’re iterating over a range from 0 to 5,000,000,000, then each of those values is taking up resources. Even though as the programmer you efficiently write a set of repeated commands inside the loop, the application still needs to store that which is being iterated over.


What Is a Function?

It’s helpful to think of a function as a black-box that takes inputs (arguments) and produces an expected output (return value). Thus, a function very much takes on the shape of a formula or an equation, except that it goes beyond arithmetic.

I described a function as a black-box because what happens inside the function really shouldn’t matter to the user—you only need to know what’s necessary to supply to the function, and what to expect as an output. With the exception of the arguments, nothing else is accessible by the function. Similarly, with the exception of the return value, nothing inside the function is accessible outside of it.


What Is a Generator?

my_list = [n for n in range(10)]
print(my_list)
# [0,1,2,3,4,5,6,7,8,9]
my_generator = (n for n in range(10))
print(my_generator)
# <generator object <genexpr> at 0x10daaa480>

First, the syntactic difference is that the list comprehension is done with square brackets [] while the generator comprehension is performed with parenthesis (). Notice the list is all stored and readily printable while a generator points to a generator object.

They seem different, but they behave similarly when working within a loop.

for element in my_list:
print(element)
"""
0
1
2
3
... etc.
"""
for element in my_generator:
print(element)
"""
0
1
2
3
... etc.
"""

What Does the “Yield” Keyword Do?

def createGenerator():
for i in range(100):
yield i
my_generator = createGenerator()
print(my_generator)
# <generator object createGenerator at 0x102dd2480>
for i in my_generator:
print(i) # prints 0-99

So, how does this work? When the returned generator is first used—not in the assignment but the for loop—the function definition will execute until it reaches the yield statement. There, it will pause (see why it’s called yield) until used again. Then, it will pick up where it left off. Upon the final iteration of the generator, any code after the yield command will execute.

Let’s add some print code to see the sequence of events:

def createGenerator():
print("Beginning of generator")
for i in range(3):
yield i
print("After yield")
print("Before assignment")
my_generator = createGenerator()
print("After assignment")
for i in my_generator:
print(i) # prints 0-99
"""
Before assignment
After assignment
Beginning of generator
0
1
2
After yield
"""

In summary, the yield keyword modifies a function’s behavior to produce a generator that’s paused at each yield command during iteration. The function isn’t executed except upon iteration, which leads to improved resource management, and subsequently, a better overall performance. Use generators (and yielded functions) for creating large data sets meant for single-use iteration.

Better Programming

Advice for programmers.

Jonathan Hsu

Written by

I’m a black belt problem-solver (literally). I enjoy the taking on new challenges, building skills, and sharing what I’ve learned. 🥋

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade