Mindmajix

Generators & Iterators in python

Before going ahead with the concept of Generators, it is important to get your fundamentals right about “Iterables” and ‘Iterators’. In other language, when we have to iterate over a list, we create a counter for the job. Have a look at the following example:

Screenshot_7

Another way to accomplish this, is by using range() as follows:

Screenshot_8

However the best way to do this in Python is as follows:

Screenshot_9

Iteration over string can be carried out in the following manner:

Screenshot_10

Iterations in file are also simple. Look at the following file object:

Screenshot_11

It is a text document. Now in order to read lines of this file through python we can implement the following code:

Screenshot_12

Iterables in Python are objects that can be iterated over. There are many other ways to carry out iterations.
You can use a list constructor to carry out iterations:

Screenshot_13

The following example shows how sum() function can take iterables to produce a total sum: 

Screenshot_14

The next example shows how iter() function can be used for iterations:

Screenshot_15

With every next command, the next element is produced. When no more elements are left, it will display StopIteration as follows:

Screenshot_16

Coming to Generators

In the above section, you have seen various ways of carrying out iterations, however these can be further simplified by the use of Generators. A generator object is an iterator.

When a Python function is called, immediately the first line of the function is executed followed by the remaining lines of code till the point where the function ends and the control is then handed over to the caller.

We all have seen functions that return a single value at the end, but what if we have a requirement for a function that must return more than one value or a series of value? In cases where a function is expected to yield a series of values, it is important for it to save its work. Usage of ‘yield’ (as discussed in the coming examples) entails that the control is transferred temporarily and the function will reclaim it in the future.

In Python, we make use of ‘generators’ (along with ‘yield’ keyword) to achieve this ask. Generators give programmers to easily generate code which is further used to generate a series of values. With this, there is no need to have a counter to keep a track of states between calls. Simple generators are often referred to as coroutines.

The main difference between a generator and an iterable is that the latter is capable of returning only one number at a time.

Now, let’s have a look at how generators simplify the work of iteration.

Screenshot_17

When a call is made to the generator function, it first returns a generator object. By this time not even the first line of the generator has been executed. The function starts executing when the next() method is called. The function is executed till it encounters ‘yield’ and returns the value that it has received at that point. To have a better idea about how this happens, we can put some print statements in the above example:

Screenshot_18

So, generators are like a special kind of iterator that can be used to generate a series of values. The ‘yield’ in a generator function is equivalent to ‘return’ in normal function, it helps in saving the ‘state’ of the generator function. The next value of the generator can be called by using ‘next()’ function.

Enroll for Instructor Led Live Python Training


0 Responses on Generators & Iterators in python"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.