Difference Between Iterable vs Iterator vs Generator

#python #programming #datascience

Akash Borgalli May 26 2021 · 3 min read
Share this

Assumption:

We think that Iterable object and Iterator object are the same but that’s not true. They both have different functionalities and uses. In this article, we are going to deep-dive into these topics and understand them clearly.

Figure 1

Iterable Objects:

It is a kind of object or entity from which you can extract elements with the help of indexes. It returns elements after the complete iteration over the items doesn’t take place. It holds all the data in memory. Iterable objects can be accessed by using for loop. Iterable is the Superclass of Iterator. Examples of Iterable are Strings, Lists, Tuples, Sets, Dictionaries.

Figure 2

The Advantage of Iterable Object is that it works well with smaller datasets.

Iterator Objects:

Iterator is a kind of object that remembers the last returned element from the collection or Iterable object passed to it and also which element to be returned next. To convert Iterable object into Iterator use iter() and to access one by one elements from it use next(). Iterator is lazy by nature because they won’t return values until and unless we don’t ask for a value calling next function. Iterator is a subclass of Iterator.

Figure 3

Note: If the object is iterable then only you can convert that object into Iterator using iter and next keyword.

Now, Let’s try to understand the Internal Mechanism of For Loop

For loop internally converts the iterable object into iterator and then gives us the data one by one using the next function also remembering when to stop iterating over the given iterable object. In short, It is aware of the length of the object. This is how internally for loop functions.

Figure 4

Code Representation of For Loop:

Figure 5

The disadvantage of For loop is that you won’t be able to get the result or output until and unless the whole execution of for loop doesn’t end.

What is Generator Function?

The generator doesn't store the entire data. It behaves the same as Iterator but in a better way. Also, it keeps giving you the output at runtime and doesn’t wait for the entire output to be generated as we saw in for loop. The generator is implemented using a function. It is used to create a custom Iterator. To create a generator function use keyword yield and access data using next(). It basically saves the state of the function and resumes once called whereas a function that has a return statement returns the value and then terminates. A generator is a subclass of Iterator.

Figure 6

If we try to see the memory consumption of the generator function you won’t get the description for it by the MemProfiler module which is used to monitor the memory usage of a python program because the generator function tries to utilize the memory when a function is called and then releases it.

Figure 7

Now in the case of a normal return function when you are trying to generate one million records as dictionaries. You will see a detailed description of memory consumption utilized by the return function.

Figure 8
Figure 9

As we can see clearly the memory consumption of the return function as it's been highlighted in figure 9.

Advantage of using Generator Function:

1. It saves Computation time and Memory space because it doesn’t hold all values in memory.

2. It works well with large datasets.

3. Loads data a lot faster compared to Iterators

  For More Examples Please visit my GithubLink

Thanks for reading!

Keep Learning!

Comments
Read next