Generators are just like functions with one difference being that using yield instead of the return statement. Generators are memory-efficient expressions for creating a large list of items. It is used to create an infinite sequence or stream. The main quality of the generator is that it remembers the last state of function each time the next() called it resumes from where it left last time. The generator function returns an iterator.
Let’s create a sample_generator() function:
def sample_generator():
num = 1
while num < 11:
yield num
num += 1
for i in sample_generator():
print(i, end = " ")
Output: 1 2 3 4 5 6 7 8 9 10
A return statement completely terminates the function while the yield statement freezes the function saving all the states and later continuing from the last value.
Let’s see one example of string reverse using a Generator:
def reverse_string(sample):
l = len(sample)
for i in range(l-1,-1,-1):
yield sample[i]
for ch in reverse_string("adams"):
print(ch, end = "")
Output: smada
Let’s execute one example to understand the difference in detail:
import sys
# Create sample list
sample_list = [1, 2, 3, 5, 10, 15]
# create list comprehension
list_comprehension = [x**3 for x in sample_list]
# Create generator expression
test_generator = (x**3 for x in sample_list)
# Print the size of list comprehension and generator
print("Size of List Comprehension:", sys.getsizeof(list_comprehension))
print("Size of Generator:", sys.getsizeof(test_generator))
Output: Size of List Comprehension: 120 Size of Generator: 112
In this tutorial, we will focus on MapReduce Algorithm, its working, example, Word Count Problem,…
Learn how to use Pyomo Packare to solve linear programming problems. In recent years, with…
In today's rapidly evolving technological landscape, machine learning has emerged as a transformative discipline, revolutionizing…
Analyze employee churn, Why employees are leaving the company, and How to predict, who will…
Airflow operators are core components of any workflow defined in airflow. The operator represents a…
Machine Learning Operations (MLOps) is a multi-disciplinary field that combines machine learning and software development…