Difference between containers, iteration, iterables, iterators and generators in Python

#generators #containers #iteration #iterables #iterators

Vinay Patil Nov 20 2021 · 3 min read
Share this

So you have come to this blog to learn about containers, iteration, iterables, iterators and generators in Python after viewing more than 10 articles and Youtube videos. Right?

I too have read more than 10 articles and Youtube videos and scratched by head in learning these topics. So I thought I should a write a blog on this topic in easy and elegant way so everyone can understand these topics easily.

There is so much to write on these topics but it will confuse everyone so I will try to explain all these topics in a very simple way. Let's Start.

Containers :

Containers are objects which holds or contains any number of other objects.

eg. Consider it as a box in which different objects are kept.

lists, tuples, sets, strings, dicts, etc. are the build in containers in python.

Iteration :

It is a process in which objects in the containers are accessed or used one by one.

eg. Objects placed in a Box will be accessed or used one by one in line.

Iterables :

Iterables are objects which contains other objects or elements. Their elements can be accessed one by one. We can move from one element to other i.e we can iterate over.

eg. If we can access the objects placed in a box one by one, then that box can be called as Iterables. lists, tuples, sets, strings, dicts, etc are Iterables. All iterables are containers.

To perform an Iteration process on iterables we can use loops or iterators or generators.

First let’s use a for loop to perform an iteration process on a list.

In above for loop we performed an iteration on List1 (iterable) and we accessed all elements of the list at the same time. In this, computer’s memory is consumed for all the elements which we have accessed. It’s easy, isn’t it?

In the above eg. only 4 elements are present in the list. Now the problem comes if we have millions of elements present in the list. If we use for loop on that list, it will access all the millions elements at the same time and computer’s memory will be used for all accessed elements at the same time.

Now this will create problem for us as we will run out of memory and our system will hang.

Box eg. In the above operation we will need memory to store all the objects or elements present in the box.

This is where iterators comes into the picture, which will be helpful in this situation.

Iterators:

Iterators are objects, used to perform iteration on iterables like list, tuple, sets, strings, etc.

It uses 2 methods iter() and next()

iter() is used to convert a iterable to iterator.

next() is used to access the elements of the iterator one by one in sequence.

If we write iter(List1) it will convert List1 (iterable) to iterator and then we can access all elements one by one using next() again and again. Let's see how iterators work.

In above eg. We converted the List1 (iterable) to iterator using iter() and stored it in List2. Now List2 will be an iterator (not iterable). If we write List2 , o/p will show it is an iterator.

We can access all the elements of the iterator one by one by using next() function again and again. Once it accesses all the element it will give StopIteration error which says there are no more elements left in the iterator.

As we can see, iterator doesn’t access all the elements at the same time because of which memory is used only for 1 element which is accessed at a time.

Box eg. We will need memory to store just one object or element of the box at a time.

This is the main advantage of iterators, it is memory efficient.  

We can even use for loops in iterators to access the elements but I am not going to discuss that as it will create more confusion.

Generators :

Python Generators are iterators. Generators are used just like iterators and are used to perform iteration. They are mostly used in loops to generate an iterator by returning all the values in the loop without affecting the iteration of the loop. It uses a function and yield keyword instead of return which is normally used in the function body . 

In above eg. we have created a generator by creating a normal function squares (n) and using yield in the body of the function instead of return. So whenever we see yield keyword in the function we can say the function is a generator function. Yield keyword returns a value (squares of the numbers) without exiting the function. Yield maintains the state between function calls, and resumes from where it left off when we call the next() method again. So if yield is called in the generator, then the next time the same generator is called we'll pick right back up after the last yield statement.

In above eg. We have created a generator function (squares(n))  and assigned it to variable t and if we type t , we can see that variable t is converted to generator object.   We can access all the elements of the generator function one by one by using next() function again and again. Once it accesses all the element it will give StopIteration error which says there are no more elements left in the iterator.

Advantages of Generators :

  • It saves the local variable (maintains local state) means generators remembers where it left the last process and it will start from the same place whenever we call the function using next() again.
  • They are memory efficient as it uses memory for only element which is been accessed at that time.
  • Yield keyword can be used many times in the same function and different outputs can be generated using same generator function.
  • We can write simple and compact code using generators.
  • Important Points :

  • All iterables are containers.
  • Loops, Iterators, Generators are used to perform iteration on iterables.
  • Iterators uses iter () and next () methods.
  • Generators uses function and yield keyword.
  • All generators are iterators (but all Iterators are not generators).
  • Comments
    Read next