How Python Generators Made My Code Faster

Tola Ore-Aruwaji
3 min readJul 2, 2021

--

Photo by Jayphen Simpson on Unsplash

In this guide, we will be looking at Python generators and how it reduced my memory usage and made my code execution faster.

After this tutorial, You will know why you should use them, when to use them and the benefits they offer over lists.

Let's dive in using different scenarios 🌟

From the code below, we have the function called square_numbers it takes in a list of numbers.

  • The result variable is set to an empty list.
  • We looped through the entire numbers from list that we passed. After that, we append the square of the numbers to the result list.
  • When the looping is done, we return the result.
  • From the numbers variable we have it set to 1, 2, 3, 4, 5 were we passed in a list of 1, 2, 3, 4, 5
  • Then we printed out the numbers. Run your code on your editor or terminal you will see the result of the numbers printed out.

From this example, our square_numbers functions is returning a list. So how can we convert this to be a generator?

To convert the list to a generator, we won't need the result variable anymore, all we have do is to use the yield function to yield the square number.

The yield keyword is what makes it a generator. When you run the code you will notice that whenever i print my_nums we are no longer getting the result of the square of the numbers instead we are seeing the results of the generator object.

The reason for this is that generators don’t hold the entire result in memory, it yields one result at a time. It then waits for the next result of the code.

i is equal to 1 which is the first value, so we yielded out 1*1 and it gave us the result. if we copy line 7 and paste it 4 times.

You can see that each time we run next it goes and gets the next value that yielded. 25 is the last value from our result and if we run next again the 6th time, we will get an error on your terminal which says stop iteration

That means that the entire generator has been exhausted, stop interation which means that it is out of value. Instead of getting the values on at a time, we could use a for loop

One huge advantage over a list, is that this is much more readable than setting the results to an empty list and appending to that result and then returning the result. This is much more readable because we are passing in the numbers and for each number in that list of numbers yield the result.

We can also implement the strategy of a list comprehension to generate our generator.

In the code below, we are creating a list and taking x*x for x in the list of 1, 2, 3, 4, 5 save it and run the code you will get the same result and you can also print out the list at the top.

You can create a generator in the same way by taking out the bracket and using parenthesis.

If you run it and try to print it out all at once, you get the generator object.

Then you for loop will loop through and print out the result.

Print it out as a list by typing:

my_nums = (x*x for x in [1, 2, 3, 4, 5]) print list(my_nums)

But when you convert a generator to a list you do lose the advantages that you gain in terms of performance.

A generator is better in performance because it doesn't hold the values at all. If the generator had a lot of values that needed to convert to that list then you lose the performance in terms of it will put all of those values into memory.

Enjoyed the read? Please leave a comment if you have any thoughts about the topic. Thanks for reading. ❤️ You can also reach out to me on Twitter or find me on Github.

More content at plainenglish.io

--

--

Tola Ore-Aruwaji
Tola Ore-Aruwaji

Written by Tola Ore-Aruwaji

Software Craftsman. DevOps & Cloud Advocate.

No responses yet