Generators in Python

A guide to understanding the use and working of generators in Python

Jayashree domala
4 min readOct 19, 2020

Generator functions allow us to write a function in such a way that it can return back value and then later resume to pick up where it left off. So basically it allows us to generate a sequence of values over time.

So what happens when a generator function is compiled? They become an object that supports an iteration protocol, meaning that when they are called in the code, they don’t actually return a value and then exit. Instead, they automatically suspend and resume the execution state around the last point of value generation.

The advantage of this is that instead of having to compute an entire series of values upfront and hold it in memory, the generator computes one value and waits until the next value is called for.

For example: range() function does not produce a list of values from start to stop in the memory. Instead, it keeps a track of the last number and step size to produce the next number and to provide the flow of numbers.

Let’s start off by creating a simple function.

>>> def create_squares(n):
list1 = []
for i in range(n):
list1.append(i**2)
return list1
create_squares(5)
[0, 1, 4, 9, 16]

So while creating this function, we had to make an empty list, then iterate through the numbers and append in the list. So this entire list is saved in the memory. This is useful when we need the entire list at once. But what if we needed to print the values individually like below. Here we only need one value at a time. And to generate the next value, we need the current value and the function.

>>> for j in create_squares(5):
print(j)
0
1
4
9
16

So using generators, we will modify as follows which generates the values as and when needed.

>>> def create_squares(n):
for i in range(n):
yield i**2
>>> for j in create_squares(5):
print(j)
0
1
4
9
16

This generator now is just an object, if solely called then it will return an address. You can cast it into a list too.

>>> create_squares(5)
<generator object create_squares at 0x000001E713F33348>
>>> list(create_squares(5))
[0, 1, 4, 9, 16]

To further understand the generators, let’s get an idea of the “next” function and “iter” function

“next” function

>>> def func1():
for i in range(3):
yield i
>>> for j in func1():
print(j)
0
1
2

Now let’s call this function by assigning it to a variable and call it using “next” function. As you can observe below, it prints the next item according to the formula. When it reaches the last item, it stops the iteration by giving an error which means all items are yielded. The same thing happens in a “for” loop also but it automatically catches the error and stops calling.

>>> a = func1()>>> a
<generator object func1 at 0x000001E713F33248>
>>> next(a)
0
>>> next(a)
1
>>> next(a)
2
>>> next(a)
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-33-15841f3f11d4> in <module>
----> 1 next(a)
StopIteration:

“iter” function

It allows us to automatically iterate through an object.

>>> str1 = "python">>> for i in str1:
print(i)
p
y
t
h
o
n

When we try to iterate using “next”, it gives an error saying the string object is not iterable directly using the “next” function. So to iter through the string, we can use the “iter” function.

>>> next(str1)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-37-dbc6a33f5b05> in <module>
----> 1 next(str1)
TypeError: 'str' object is not an iterator>>> str1_iter = iter(str1)>>> next(str1_iter)
'p'
>>> next(str1_iter)
'y'

So now after using the “iter” function, you are able to use “next” function.

Refer to the notebook here.

Beginner-level books to refer to learn Python:

Advance-level books to refer to learn Python:

Reach out to me: LinkedIn

Check out my other work: GitHub

--

--

Jayashree domala

Self-driven woman who wishes to deliver creative and engaging ideas and solutions in the field of technology.