What are Generators in Python and how to use them?

Aayushi Johari
Edureka
Published in
6 min readJul 15, 2019
Generators in Python — Edureka

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:

  • What are the Generators?
  • Advantages of using Generators
  • Normal Functions vs Generator Functions
  • Using Generator Functions
  • Generators with loops
  • Generator Expressions
  • Use Cases
  1. Generating Fibonacci Series
  2. Generating Numbers

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 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 the 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:

The above image shows the execution of our program required a 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:

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 that 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.

If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series which will explain the various other aspects of Python and Data Science.

1. Machine Learning Classifier in Python

2. Python Scikit-Learn Cheat Sheet

3. Machine Learning Tools

4. Python Libraries For Data Science And Machine Learning

5. Chatbot In Python

6. Python Collections

7. Python Modules

8. Python developer Skills

9. OOPs Interview Questions and Answers

10. Resume For A Python Developer

11. Exploratory Data Analysis In Python

12. Snake Game With Python’s Turtle Module

13. Python Developer Salary

14. Principal Component Analysis

15. Python vs C++

16. Scrapy Tutorial

17. Python SciPy

18. Least Squares Regression Method

19. Jupyter Notebook Cheat Sheet

20. Python Basics

21. Python Pattern Programs

22. Web Scraping With Python

23. Python Decorator

24. Python Spyder IDE

25. Mobile Applications Using Kivy In Python

26. Top 10 Best Books To Learn & Practice Python

27. Robot Framework With Python

28. Snake Game in Python using PyGame

29. Django Interview Questions and Answers

30. Top 10 Python Applications

31. Hash Tables and Hashmaps in Python

32. Python 3.8

33. Support Vector Machine

34. Python Tutorial

Originally published at https://www.edureka.co on July 15, 2019.

--

--

Edureka
Edureka

Published in Edureka

There are many e-learning platforms on the internet & then there’s us. We are not the biggest, but we are the fastest growing. We have the highest course completion rate in the industry. We provide live, instructor-led online programs in trending tech with 24x7 lifetime support.

Aayushi Johari
Aayushi Johari

Written by Aayushi Johari

A technology enthusiast who likes writing about different technologies including Python, Data Science, Java, etc. and spreading knowledge.

No responses yet