Exploring Iterator, Generator and Collections of python

In our python learning series, we will continue some of the other modules which are going to help in learning graph. In this series, we will learn about the Iterator concept of python. Before going to an explanation about the Iterator, we need to understand what are the loops available in python. I am not going into the details of loops here because we have already gone through my last tutorial. Here is the link to explore.  Conditional Control and Data Structure (List, Dictionary, and Set) of python.

Now let’s learn about the iterator of python. Iterator means executing the same block of code over and over, potentially many times something related with a loop. The question is why Iterator? while loops conditional already available in python. I would say in a simple word that  If you handled with huge data, normal operations will cause trouble. Iterators or Generators take value one by one and less load on memory. In that case, we would prefer to use the Iterator.

Let’s see what are the function and keyword are available to use the iterator.

Iterator:
Use ‘iter()’ to create an iterator
‘iterkeys()’, ‘itervalues()’, ’iteritems()’ for dict
Iter.next() to read next value
Raises ‘StopIteration’ exception at the end
Raises TypeError exception is input type not supported

Generators:
Functions that work as iterators
Use ‘yeild’ function for generator

Let’s see a few examples to understand the iterator in detail. Suppose we have list and want to iterate this list by Iterator.

Output :

So in the above example, we can see that iterator only print the item which is available in the next loop else it will be stopped. Now let us see the few examples of Iterator for the dictionary. We can iterate by key or by value or key and value both. Please see the below example.

Out something like:

Let’s see an example of a Generator. A generator can be defiled like this

Example for same above definition.

Some of another thing or external module Collections which are related to Iterator, we can use like Collections: counter and Collections: deque.  A counter is Subclass of dict for counting object which is initialization: similar to dict. Let’s explore some of the frequently used methods.

Collections: counter

Elements() : return iterator over elements
Most_common([n]): most common ‘n’ elements
Subtract([iterable]): subtracts count.
Update([iterable]): add the counts

Let’s see a few examples to understand in detail.

Collections: Deque: (double-ended-queue)

Deque supporting stack and queue operations and Initialization collections.deque([iterable]).

Methods:

append(x): append at end
appendleft(x): append to beginning
extend([iterable]): append iterable to end
extendleft([iterable]): append to left/beginning from iterable
pop(): remove and return from end
popleft(): remove and return from beginning

Let’s see an example to understand Collection Deque.

Output:

So we can see that the Collections Module have an awesome feature. We can also make the Collections in default Dictionary or NamedTuple or ordered dictionary. So I mean if you have Collections module then we can use default feature of python. Let us a few examples to clearly understand.

An Example for orderedDict.

OutPut:

Summary: Now we have a good understanding of the Iterator and Collections module. We have seen a few examples and play around it to clear the doubts. If you still having issue or doubts then please add a comment I will try to reply as much possible. In my next python tutorial series, we will learn about the Database connections with the example.

If you are wondering to learn Android then Please Learn from Android category and Wondering to lean Kotlin then Kotlin Category will help you.

Please do subscribe your email to get the newsletter on this blog on below and if you like this post then do not forget to share like and comment on the below section.

Happy Coding 🙂

 

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.