Open In App

Generators in Python

Last Updated : 06 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A Generator in Python is a function that returns an iterator using the Yield keyword. In this article, we will discuss how the generator function works in Python.

Generator Function in Python

A generator function in Python is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a Python generator function. 

Create a Generator in Python

In Python, we can create a generator function by simply using the def keyword and the yield keyword. The generator has the following syntax in Python:

def function_name():
yield statement

Example:

In this example, we will create a simple generator that will yield three integers. Then we will print these integers by using Python for loop.

Python3




# A generator function that yields 1 for first time,
# 2 second time and 3 third time
def simpleGeneratorFun():
    yield 1            
    yield 2            
    yield 3            
   
# Driver code to check above generator function
for value in simpleGeneratorFun(): 
    print(value)


Output:

1
2
3

Generator Object

Python Generator functions return a generator object that is iterable, i.e., can be used as an Iterator. Generator objects are used either by calling the next method of the generator object or using the generator object in a “for in” loop.

Example:

In this example, we will create a simple generator function in Python to generate objects using the next() function.

Python3




# A Python program to demonstrate use of 
# generator object with next() 
  
# A generator function
def simpleGeneratorFun():
    yield 1
    yield 2
    yield 3
   
# x is a generator object
x = simpleGeneratorFun()
  
# Iterating over the generator object using next
  
# In Python 3, __next__()
print(next(x))
print(next(x))
print(next(x))


Output:

1
2
3

Example:

In this example, we will create two generators for Fibonacci Numbers, first a simple generator and second generator using a for loop.

Python3




# A simple generator for Fibonacci Numbers
def fib(limit):
      
    # Initialize first two Fibonacci Numbers 
    a, b = 0, 1
  
    # One by one yield next Fibonacci Number
    while a < limit:
        yield a
        a, b = b, a + b
  
# Create a generator object
x = fib(5)
  
# Iterating over the generator object using next
# In Python 3, __next__()
print(next(x)) 
print(next(x))
print(next(x))
print(next(x))
print(next(x))
  
# Iterating over the generator object using for
# in loop.
print("\nUsing for in loop")
for i in fib(5): 
    print(i)


Output:

0
1
1
2
3

Using for in loop
0
1
1
2
3

Python Generator Expression

In Python, generator expression is another way of writing the generator function. It uses the Python list comprehension technique but instead of storing the elements in a list in memory, it creates generator objects.

Generator Expression Syntax

The generator expression in Python has the following Syntax:

(expression for item in iterable)

Example:

In this example, we will create a generator object that will print the multiples of 5 between the range of 0 to 5 which are also divisible by 2.

Python3




# generator expression
generator_exp = (i * 5 for i in range(5) if i%2==0)
  
for i in generator_exp:
    print(i)


Output:

0
10
20

Applications of Generators in Python 

Suppose we create a stream of Fibonacci numbers, adopting the generator approach makes it trivial; we just have to call next(x) to get the next Fibonacci number without bothering about where or when the stream of numbers ends. A more practical type of stream processing is handling large data files such as log files. Generators provide a space-efficient method for such data processing as only parts of the file are handled at one given point in time. We can also use Iterators for these purposes, but Generator provides a quick way (We don’t need to write __next__ and __iter__ methods here).



Previous Article
Next Article

Similar Reads

Using Generators for substantial memory savings in Python
When memory management and maintaining state between the value generated become a tough job for programmers, Python implemented a friendly solution called Generators. With Generators, functions evolve to access and compute data in pieces. Hence functions can return the result to its caller upon request and can maintain its state. Generators maintai
12 min read
Memory Efficient Python List Creation in Case of Generators
We have the task of writing memory-efficient Python list creation in the case of Generators and printing the result. In this article, we will demonstrate memory-efficient Python list creation using Generators. What are Generators?Generators in Python are a type of iterable, allowing the creation of iterators in a memory-efficient manner. Unlike lis
3 min read
Writing Memory Efficient Programs Using Generators in Python
When writing code in Python, wise use of memory is important, especially when dealing with large amounts of data. One way to do this is to use Python generators. Generators are like special functions that help save memory by processing data one at a time, rather than all at once. The logic behind memory-efficient functions and Python generators is
5 min read
CNN - Image data pre-processing with generators
The article aims to learn how to pre-processing the input image data to convert it into meaningful floating-point tensors for feeding into Convolutional Neural Networks. Just for the knowledge tensors are used to store data, they can be assumed as multidimensional arrays. A tensor representing a 64 X 64 image having 3 channels will have its dimensi
3 min read
Important differences between Python 2.x and Python 3.x with examples
In this article, we will see some important differences between Python 2.x and Python 3.x with the help of some examples. Differences between Python 2.x and Python 3.x Here, we will see the differences in the following libraries and modules: Division operatorprint functionUnicodexrangeError Handling_future_ modulePython Division operatorIf we are p
5 min read
Python program to build flashcard using class in Python
In this article, we will see how to build a flashcard using class in python. A flashcard is a card having information on both sides, which can be used as an aid in memoization. Flashcards usually have a question on one side and an answer on the other. Particularly in this article, we are going to create flashcards that will be having a word and its
2 min read
Python | Merge Python key values to list
Sometimes, while working with Python, we might have a problem in which we need to get the values of dictionary from several dictionaries to be encapsulated into one dictionary. This type of problem can be common in domains in which we work with relational data like in web developments. Let's discuss certain ways in which this problem can be solved.
4 min read
Reading Python File-Like Objects from C | Python
Writing C extension code that consumes data from any Python file-like object (e.g., normal files, StringIO objects, etc.). read() method has to be repeatedly invoke to consume data on a file-like object and take steps to properly decode the resulting data. Given below is a C extension function that merely consumes all of the data on a file-like obj
3 min read
Python | Add Logging to a Python Script
In this article, we will learn how to have scripts and simple programs to write diagnostic information to log files. Code #1 : Using the logging module to add logging to a simple program import logging def main(): # Configure the logging system logging.basicConfig(filename ='app.log', level = logging.ERROR) # Variables (to make the calls that follo
2 min read
Python | Add Logging to Python Libraries
In this article, we will learn how to add a logging capability to a library, but don’t want it to interfere with programs that don’t use logging. For libraries that want to perform logging, create a dedicated logger object, and initially configure it as shown in the code below - Code #1 : C/C++ Code # abc.py import logging log = logging.getLogger(_
2 min read
Article Tags :
Practice Tags :