Tips For Python

The one with the generators!

Ignacio Ruiz
3 min readJun 10, 2021

Hey! welcome back! I wanted to bring to show something very cool. Its called a generator. Basically a generator functions allow you to declare a function that behaves like an iterator, i.e. it can be used in a for loop.

I have introduced you to generators before in the article about yield functions. Today we will look another way to improve our code by using generators and substitute our list comprehensions.

The performance improvement from the use of generators is the result of the lazy (on demand) generation of values, which translates to lower memory usage. Furthermore, we do not need to wait until all the elements have been generated before we start to use them. This is similar to the benefits provided by iterators, but the generator makes building iterators easy.

W e can turn a list comprehension into a generator expression by replacing the square brackets (“[ ]”) with parentheses. Alternately, we can think of list comprehensions as generator expressions wrapped in a list constructor.

Lets look at this examples:

Notice how a list comprehension looks essentially like a generator expression passed to a list constructor.

By allowing generator expressions, we don’t have to write a generator function if we do not need the list. If only list comprehensions were available, and we needed to lazily build a set of items to be processed, we will have to write a generator function.

This also means that we can use the same syntax we have been using for list comprehensions to build generators.

Say, we had to compute the sum of the first n, say 1,000,000, non-negative numbers, we will build it as:

When we use range, we do not incur the cost of building a 1,000,000 element list in memory. The generator created by xrange will generate each number, which sum will consume to accumulate the sum.

In the case of the “range” function, using it as an iterable is the dominant use-case, and this is reflected in Python 3.x, which makes the range built-in return a sequence-type object instead of a list.

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

There you go! a quick overview of how you can use generators to help you in your python coding journey! Try it out!

--

--