Lesson 18. Generators

To understand Generator, you should be familiar with Iterator first, because Python Generator is an elegant approach to create an iterator. An Iterator in a nutshell is pointer that points to first element of a collection like list, tuple, or dictionary. Iterator is a class with iter() and next() methods. When we pass iterator object to python's next() function, it points to next element in that collection. When end of iterator is reached an StopIteration is raised, letting you know that are are no further elements. You already see the pain in implementation. This is where the saviour Generator comes into play.

So, a Generator is a function that that uses yield keyword to create an object that could be iterated using next() function. Generator is just like other python functions, but with yield statment instead of return. The difference here is that when return statement is executed, the function stops execution right away which is not the case with yield statement. It keeps generating values and resumes on successive calls. The Generator remembers all local variables when called by subsequent call.

Now, you must be wondering how generators are different from comprehension (Lesson no 19). Well, comprehensions produce the entire list, but generators only give you one element at a time, this is known as lazy operation. Isn't it memory ifficient?

There are two different ways you can create generators in python.

  1. Generators can be created like normal functions with only modification of using yield statment instead of return statement. This is the common way of creating generators.

  2. Generators can be created using comprehension like method using parenthesis instead of square brackets.

Generators Syntax

#Function method
def some_generator_function(some_argument):
    #some expressions
    #and yeild statements
    yield "some variable"
    
#Expression method
generator = (some_function(x) for x in some_collection)

Examples

Example #1: Function Method

Let's create an iterator using Generator by reversing Some company's revenue in million dollars for one week.

def reverse_revenue_generator(x):
    i = len(x)-1
    while i>=0:
        yield x[i]
        i-=1

#company's revenue
revenue = [3,4,10,11,18,21,39]

for rev in reverse_revenue_generator(revenue):
    print(rev)

Example #2: Expression Method

Using the expression method.

# creating a list of numbers
some_vals = [1, 16, 81, 100]

# take square root of each term
list_ = [x**0.5 for x in some_vals]

# This can be done using a generator expression
# generator expressions are surrounded by parenthesis ()
genr = (x**0.5 for x in some_vals)

print(list_)
print(*genr)

Now you try it!

Don't copy and past. Type the code yourself!

Last updated