Python Generator – Speed Up Your Code

In Python, Generator functions are special functions that return a lazy iterator (or an iterator that is only used when you call it). Lazy iterators are objects that you can loop over like a list. However, unlike lists, lazy iterators do not store their items in memory. This makes them memory efficient when working with large data.

# Generator function for an infinite sequence
def my_generator():
    x = 0
    while True:
        yield x
        x += 1

next(g)
>>> 0
next(g)
>>> 1

The difference is that while a return statement terminates a function entirely, yield statement pauses the function saving all its states and later continues from there on successive calls.

Python Generator

The performance improvement from the use of python generators is the result of on demand generation of values. This means we don’t need to wait for values to be generated to use them. We can create and use then one by one.

Note: Generator will provide performance benefits only if we do not intend to use that set of generated values more than once.

Generators are an advanced Python topic. 99% of the time regular functions (with return statements) will work for you code. If you are running into performance problems, consider using generators with your loops.

Let’s take a look at a python generator code sample


Python Generator

Generators are like functions, but especially useful when dealing with large data. Generators will turn your function into an iterator so you can loop through it.

To create a generator, you must use yield instead of return. Generators will remember states. This means the function will remember where you left off.

Generators are an advanced Python topic, only use these if you know what you are doing! If not, 99% of the time normal functions will be fine.

In [23]:
# Generator function
def my_generator():
    x = 1
    print('Printed: First')
    yield x

    x = x + 1
    print('Printed: Second')
    yield x

    x = x + 1
    print('Printed: Last')
    yield x
In [24]:
g = my_generator()
In [25]:
next(g)
Printed: First
Out[25]:
1
In [26]:
next(g)
Printed: Second
Out[26]:
2
In [27]:
next(g)
Printed: Last
Out[27]:
3

If you try going too far with a generator, then you'll get an error

In [28]:
next(g)
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-28-e734f8aca5ac> in <module>
----> 1 next(g)

StopIteration: 

Let's create another with an infinate loop

In [29]:
# Generator function
def my_generator():
    x = 0
    while True:
        yield x
        x += 1
In [30]:
g = my_generator()
In [31]:
next(g)
Out[31]:
0
In [32]:
next(g)
Out[32]:
1
In [33]:
next(g)
Out[33]:
2
In [34]:
next(g)
Out[34]:
3
In [35]:
next(g)
Out[35]:
4

Look how we have access to an infinite set of numbers, but they aren't stored in memory anywhere. They are created and called one by one because of the generator. Cool!

Link to code above

Check out more Python Vocabulary on our Glossary Page