A generator function is a function which returns the generator- iterator with the help of yield function. It means that a generator generates iterators. In simple words, a generator produces the result when needed, while an iterator produces the entire result. In the previous article we learned about iterators. To make a generator, yield () function is used. Let’s take the same example of squaring the list elements. But now we don’t need to append and return anything. We just need to write a yield followed by the operation to be performed. It’s more readable also. For each value present in the list, yield the square of the value. No append, no return and no need to store it in an empty list.

If we see this, the output generated is not a list of squared numbers. Generators don’t hold the entire result in the memory. It hasn’t done anything yet. It’s waiting for us to ask to give the next result. And for this next function is used. So, I’ll write my function and argument inside the next function and print it.

So, it’ll print the next result. That’s the squared value of the first element of the given list. It’s again waiting for us to ask what to do next. Let’s print the next values.

OK. All values are printed. Oh yes, we don’t need the empty list as well. What will happen if we still ask to print the next step?


Stop Iteration error occurs. It means that the generator has exhausted as there are no further values in the list to generate the next value. There is a much better approach to write so many next statements by using a for loop.

This will check for all values of elements in the given list and print the result. Basically, what we were getting with iterators in the last chapter, we are getting with generators as well. But here we have more control on executing our output.
We can also write a generator comprehension just like a list comprehension. Two things we have to remember. First, use normal parentheses instead of square brackets. Second, we know that a generator will give only one value at a time, so to get the next result we’ll write the for loop again.

These outputs are not holded up all at once in memory. What if you want to print out all the values from the generator? We can convert this into a list. If you want to convert it into a list, use the list function and pass the generator into it.

Let me take another example. Now I am making the program of the Fibonacci series as a generator function.

Everything is like what we have done before. The only difference is now we are using the yield keyword with our output. When the program executes the yield statement, it stops. When we ask it to fetch the next value, then only it’s iterating.

So what is the advantage of this generator over the iterators? The main advantage is that you can save huge heaps of memory at the cost of having extra execution time for generating the next iterable object. But the limitation of a generator function is that they are slow because they execute once. You can also convert your generator output to become a list using the list function. If you need to save memory and don’t care about execution time use a generator, and if you don’t care about high memory usage and need a little bit faster execution time use a list.
