Mastering Artificial Intelligence

Python Generators and Yield

In the previous blog, we have seen how we can build an iterator using __iter__() and __next__() method. In this blog, we will learn an easy and fast way to make our own iterator.

Generators return an iterator object in a simple fashion. Let’s see how we can make generators in Python:

Generator Function: This is the same as the normal function. The only difference is that we use yield statement instead of return. The return statement terminates the function while the yield statement pauses the function and starts from that point again whenever called. Let’s see by an example

Python Generators

Python

1

2

3

4

5

6

defOdd():

x=1

yieldx

x+=2

yieldx

The output looks like this

Python

1

2

3

4

5

6

7

8

9

10

11

12

>>>a=Odd()

>>>type(Odd())

<class'generator'>

>>>next(a)

1

>>>next(a)

3

>>>next(a)

Traceback(most recent call last):

File"<pyshell#4>",line1,in<module>

next(a)

StopIteration

The Odd() function used above is of type generator and ‘a’ is a generator object.

Calling yield, saves all states(x in above code) and continues from that point on calling again ( next(a) returns 3). To iterate again we have to create a new generator object.

We can also use generators with for loop directly.

Python

1

2

3

4

5

>>>fornum inOdd():

print(num)

1

3

So, whenever we use yield instead of return, we end up making a generator function.

Generator Expression: This is similar to the list comprehension. Only replace the square bracket with the round. Let’s see an example

Generator Expression

Python

1

2

3

4

5

6

>>>a=[iforiinrange(4)]

>>>type(a)

<class'list'>

>>>a=(iforiinrange(4))

>>>type(a)

<class'generator'>

Pros: The generator expression gives one element at a time(Lazy evaluation), unlike list comprehensions that output the entire list. Thus, generator expressions are more memory efficient than list comprehensions.

To find the next element in generator expression, use the next() function. Let’s take above code