What are generator yields?

A Python generator is a function that returns a generator iteratoran object we can iterate over by calling yield. In a broader context, Python generators are able to suspend and resume their execution at the time of value generation. In simpler terms, a generator uses yield, which returns a generator object that looks something like <class 'generator'>. This is an iterable object that needs to be iterated using a loop method, a list method, etc. The object can be iterated whenever it is needed. For a single value returned by the generator, an object may use the next() keyword to access the value returned by the generator.

Generator output examples

Method 1

Let’s first look at a simple function that takes a list as a parameter, squares every number in the list, saves these squares in another list, and returns the list:

def square_it(newlist):
list_return=[]
for i in newlist:
list_return.append(i**2)
return list_return
mylist = [1,2,3,4]
# number 1 method
print(square_it(mylist))

Method 2

In this example, we convert the above function to a generator function and look at one of the methods to obtain output from the iterable object. You can see that our function square_it() takes in a list as a parameter. However, we do not need to store the square into another. Moreover, we have replaced return with a yield, so the function returns an iterable object (as can be seen by the output of variable result). However, we can view the iterable output using list(result), which iterates over the variable result and displays all the numbers as a list:

def square_it(newlist):
for i in newlist:
yield i**2
mylist = [1,2,3,4]
result = square_it(mylist)
# generator object
print(result)
# number 2 method
print(list(result))

Method 3

In this example, we will use a for loop to iterate over each value in the variable result:

def square_it(newlist):
for i in newlist:
yield i**2
mylist = [1,2,3,4]
result = square_it(mylist)
# number 2 method
for x in result:
print(x)

Method 4

In this example, we will make use of next() to access the iterable object returned by the generator function. By default, next() only returns a single; so, if our list size is 4, we will have to write next(result) 4 times (as shown in the code below) to access all the elements of the iterator object.

“What happens if I call next(result) the fifth time?”

Well, in this case, it will give a StopIteration error. This is because generators are single iteration objects. If the values have already been accessed once, the iteration stops and they cannot be accessed again. This also applied to the scenario where a user has iterated using a loop and then tries to access it again.

def square_it(newlist):
for i in newlist:
yield i**2
mylist = [1,2,3,4]
result = square_it(mylist)
# number 3 method
print(next(result))
print(next(result))
print(next(result))
print(next(result))
print(next(result))
svg viewer

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved