Search for Courses, Topics

Python Generators

Learn about Python Generators.

23 Feb 2022-5 mins read
quiz
Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Overview

Generators in Python are used to create iterators and return a traversal object. It helps in traversing all the items one at a time with the help of the keyword yield.

Scope of Article

  • In this article, we will be focussing on the generator function in Python and how generator functions are different from regular functions in Python.
  • Later, we will see how Generator functions are used with loops and different uses of generator functions in python programming.

What are Python Generators?

Python's generator functions are used to create iterators(which can be traversed like list, tuple) and return a traversal object. It helps to transverse all the items one at a time present in the iterator.

Generator functions are defined as the normal function, but to identify the difference between the normal function and generator function is that in the normal function, we use the return keyword to return the values, and in the generator function, instead of using the return, we use yield to execute our iterator.

Example:

def gen_fun():
    yield 10
    yield 20
    yield 30
    
for i in gen_fun():
    print(i)
10
20
30

In the above example, gen_fun() is a generator function. This function uses the yield keyword instead of return, and it will return a value whenever it is called.

Yield vs Return

YieldReturn
It is used in generator functions.It is used in normal functions.
It is responsible for controlling the flow of the generator function. After returning the value from yield, it pauses the execution by saving the states.Return statement returns the value and terminates the function.

Difference Between Generator Function & Normal Function

  • In generator functions, there are one or more yield functions, whereas, in Normal functions, there is only one function
  • When the generator function is called, the normal function pauses its execution, and the call is transferred to the generator function.
  • Local variables and their states are remembered between successive calls.
  • When the generator function is terminated, StopIteration is called automatically on further calls.

Python Generators With a Loop

The Generator functions are also used for the loop.

Example:

def seq(x):
    for i in range(x):
        yield i
      
range_ = seq(10)
print(next(range_))
print(next(range_))
print(next(range_))
print(next(range_))
print(next(range_))
print(next(range_))
print(next(range_))
print(next(range_))
print(next(range_))
print(next(range_))
print(next(range_)) 

Output:

0
1
2
3
4
5
6
7
8
9
Traceback (most recent call last):
  File "abc.py", line 17, in <module>
    print(next(range_))
StopIteration

The function seq will run 10 times, and when seq is called using next for the 11th time, it will show an error StopIteration.

But when using for loop, instead of next.

for i in seq(5):
    print(i)

Output:

0
1
2
3
4

It will not show any error. for loop automatically handle the exception statements.

How To Create Generator Function in Python?

In Python, there are some ways to create a generator function.

  1. Using a Yield statement
  2. Using Python Generator Expression

1. Yield Statement

Yield keyword in Python that is used to return from the function without destroying the state of a local variable. We have already discussed how to create a generator function using yield.

2. Python Generator Expression

Generator Expression is a short-hand form of a generator function. Python provides an easy and convenient way to implement a Generator Expression.

According to its definition, they are similar to lambda function as lambda function is an anonymous function, and generator functions are anonymous.

But when it comes to implementation, they are different, they are implemented similarly to list comprehension does, and the only difference in implementation is, instead of using square brackets('[]'), it uses round brackets('()').

The main and important difference between list comprehension and generator expression is list comprehension returns a list of items, whereas generator expression returns an iterable object.

Example:

x = 10
gen = (i for i in range(x) if i % 2 == 0)

list_ = [i for i in range(x) if i % 2 == 0]

print(gen)
print(list_)
for j in gen:
    print(j)

Output:

<generator object <genexpr> at 0x000002471CF1FAC0>
[0, 2, 4, 6, 8]
0
2
4
6
8

(i for i in range(x) if i % 2 == 0) in this generator expression, the first part is the yield part, and next is the for loop, followed by the if statement.

Use of Generators in Python

1. Easy to Implement:

Generator functions are easy to implement as compared with iterators. In iterators, we have to implement iter(), __next__() function to make our iterator work.

2. Memory Efficient:

Generator Functions are memory efficient, as they save a lot of memory while using generators. A normal function will return a sequence of items, but before giving the result, it creates a sequence in memory and then gives us the result, whereas the generator function produces one output at a time.

3. Infinite Sequence:

We all are familiar that we can't store infinite sequences in a given memory. This is where generators come into the picture. As generators can only produce one item at a time, so they can present an infinite stream of data/sequence.

def infinite():
    n = 0
    while True:
        yield n
        n += 1
        
for i in infinite():
    if i%4 == 0:
        continue
    elif i == 13:
        break
    else:
        print(i)

Output:

1
2
3
5
6
7
9
10
11

Conclusion

  • Generator functions in Python are used to create iterators and return an iterator object.
  • In the generator function, we use a yield statement instead of a return statement.
  • We can use multiple yield statements in the generator function.
  • When iterating over the generator function using next, in the end, it will show up StopIteration error, whereas when we use the for loop, it doesn't show any error.
  • Generator Expressions are anonymous functions. They are implemented as similar to a list comprehension, and the only difference is that they use round parentheses instead of square parentheses.
Challenge Time!
quiz Time to test your skills and win rewards! Note: Rewards will be credited after the next product update.
certificate icon
Certificates
Python Tutorial
Module Certificate
Criteria
You’ll be able to claim a certificate for any course you have access to only after you’ve spent enough time learning. The time required is determined by the length of the course.
Certificates are a fantastic way to showcase your hard-earned skills to employers, universities, and anyone else that may be interested. They provide tangible proof that you’ve completed a course on Scaler Topics. In many cases they can help you claim a training reimbursement or get university credit for a course.
certificate icon
Certificates
Python Tutorial
Module Certificate
Criteria
You’ll be able to claim a certificate for any course you have access to only after you’ve spent enough time learning. The time required is determined by the length of the course.
Certificates are a fantastic way to showcase your hard-earned skills to employers, universities, and anyone else that may be interested. They provide tangible proof that you’ve completed a course on Scaler Topics. In many cases they can help you claim a training reimbursement or get university credit for a course.