Mastering Python (56 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (27 Blogs)
  • Data Science (38 Blogs)
  • Mastering Python (51 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

What are Generators in Python and How to use them?

Published on Jul 15,2019 176 Views
30 / 33 Blog from Python Fundamentals

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assement report and video recording

Generating iterables or objects that allow stepping over them is considered to be a burdensome task. But, in Python, the implementation of this painful task just gets really smooth. So let’s go ahead and take a closer look at Generators in Python.

Here is a list of all the topics covered in this article:

So let’s begin. :)

What are Generators in Python?

Generators are basically functions that return traversable objects or items. These functions do not produce all the items at once, rather they produce them one at a time and only when required. Whenever the for statement is included to iterate over a set of items, a generator function is run. Generators have a number of advantages as well.

Advantages of using Generators

  • Without Generators in Python, producing iterables is extremely difficult and lengthy.

  • Generators easy to implement as they automatically implement __iter__(), __next__() and StopIteration which otherwise, need to be explicitly specified.

  • Memory is saved as the items are produced as when required, unlike normal Python functions. This fact becomes very important when you need to create a huge number of iterators. This is also considered as the biggest advantage of generators.

  • Can be used to produce an infinite number of items.

  • They can also be used to pipeline a number of operations

Normal Functions vs Generator Functions:

Generators in Python are created just like how you create normal functions using the ‘def’ keyword. But, Generator functions make use of the yield keyword instead of return. This is done to notify the interpreter that this is an iterator. Not just this, Generator functions are run when the next() function is called and not by their name as in case of normal functions. Consider the following example to understand it better:

EXAMPLE:

def func(a):
    yield a
a=[1,2,3]

b=func(a)
next(b)

OUTPUT: [1, 2, 3] 

As you can see, in the above output, func() is making use of the yield keyword and the next function for its execution. But, for normal function you will need the following piece of code:

EXAMPLE:

def func(a):
    return a
a=[1,2,3]
func(a)

OUTPUT: [1, 2, 3] 

If you look at the above example, you might be wondering why to use a Generator function when the normal function is also returning the same output. So let’s move on and see how to use Generators in Python.

Using Generator functions:

As mentioned earlier, Generators in Python produce iterables one at a time. Take a look at the following example:

EXAMPLE:

def myfunc(a):
    while a>=3:
        yield a
        a=a+1
b =  myfunc(a)
print(b)
next(b)

When you execute the following function, you will see the following output:

OUTPUT: 4

Here, one iterable object has been returned satisfying the while condition. After execution, the control is transferred to the caller. In case more items are needed, the same function needs to be executed again by calling the next() function.


next(b)

OUTPUT: 5

On further executions, the function will return 6,7, etc. Generator functions in Python implement the __iter__() and __next__() methods automatically. Therefore, you can iterate over the objects by just using the next() method. When the item generation should terminate, Generator functions implement the StopIteration internally without having to worry the caller. Here is another example of this:

EXAMPLE:

a=2
def myfunc(a):
    while a >= 0:
        yield a
        a -= 1
b =  myfunc(a)
print(b)
next(b)

OUTPUT:

StopIteration-Generators in Python-EdurekaThe above image shows the execution of our program required number of times. If you try to call the next function again, it returns a message depicting StopIteration has been implemented. If you try to do this with normal functions, the values returned will not change or iterate. Take a look at the example below:

EXAMPLE:

def z():
    n=1
    yield n
    n=n+3
    yield n
p=z()
next(p)

OUTPUT:

StopIteration2-Generators in Python-Edureka

Generators with loops:

In case you want to execute the same function at once, you can make use of the ‘for’ loop. This loop helps iterate over the objects and after all implementations it executes StopIteration. 

EXAMPLE:

def z():
    n=1
    yield n
    n=n+3
    yield n
for x in z():
    print(x)

OUTPUT: 

1
4

You can also specify expressions to generate iterable objects.

Generator Expressions:

You can also use expressions along with the for loop to produce iterators. This usually makes the generation iterables much easy. Generator expression resemble list comprehensions and like lambda functions, generator expressions create anonymous generator functions.

Take a look at the example below:

EXAMPLE:

a=range(6)
print("List Comprehension", end=':')
b=[x+2 for x in a] 
print(b)
print("Generator expression", end=':n')
c=(x+2 for x in a) 
print(c)
for y in c:
    print(y)

 

OUTPUT:

List Comprehension:[2, 3, 4, 5, 6, 7]

Generator expression:

<generator object <genexpr> at 0x0000016362944480>

2
3
4
5
6

As you can see, in the above output, the first expression is a list comprehension which is specified within [] brackets. List comprehension produces the complete list of items at once. The next is a generator expression which returns the same items but one at a time. It is specified using () brackets.


Generator functions can be used within other functions as well. For example:

EXAMPLE:

a=range(6)
print("Generator expression", end=':n')
c=(x+2 for x in a) 
print(c)
print(min(c))

OUTPUT:

Generator expression
2

The above program prints the min value when the above expression as applied to the values of a.

Use Cases:

Let us use Generators in Python to:

  • Generate Fibonacci Series
  • Generating Numbers

Generating Fibonacci Series: 

Fibonacci series as we all know is a series of numbers wherein each number is a sum of preceding two numbers. The first two numbers are 0 and 1. Here is a generator program to generate Fibonacci series:

EXAMPLE:

def fibo():
    first,second=0,1
    while True:
        yield first
        first,second=second,first+second
for x in fibo():
    if x>50:
        break
    print(x, end=" ")

OUTPUT:

0 1 1 2 3 5 8 13 21 34

The above output shows the Fibonacci series with values less than 50. Let’s now take a look at how to generate a list of numbers.

Generating Numbers:

In case you want to generate specified list numbers, you can do it using generator functions. Take a look a look at the following example:

EXAMPLE:

a=range(10)
b=(x for x in a) 
print(b)
for y in b:
    print(y)

OUTPUT:

<generator object <genexpr> at 0x000001CBE1602DE0>

0
1
2
3
4
5
6
7
8
9

EXAMPLE:

a=range(2,10,2)
b=(x for x in a) 
print(b)
for y in b:
    print(y)

OUTPUT:

<generator object <genexpr> at 0x000001CBE1623138>
2
4
6
8

The above program has returned even numbers from 2 to 10. This brings us to the end of this article on Generators in Python. I hope you have understood all the topics.

Make sure you practice as much as possible and revert your experience.  

Got a question for us? Please mention it in the comments section of this “Generators in Python” blog and we will get back to you as soon as possible.

To get in-depth knowledge on Python along with its various applications, you can enroll for live Python Certification Training with 24/7 support and lifetime access. 

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.