Difference in Iterable and Iterator in Python:

In this blog, we’ll be learing about the differences between the iterable and iterator in python while keeing in mind the data science aspect of it.

  • Iterable – It is an object which can be itered.
  • Iterator – It is generated from an iterable object using iter() method. In iterators, we have the _next_() method which returns the succeeding item of the object.

All iterators are iterable but the vice-versa is not true. To create an iterator from an iterable the class of an object either needs _iter_() method or a _getitem_ method containing sequential indexes beginning with 0.

Example 1:

Difference in Iterable and Iterator in Python for Data Science - PST Analytic

Example 2: The function iterable returns True, when the object ‘obj’ is iterable or else it returns False.

Note: If function ‘next(iterator_object)’ is called once more then, it would return’StopIteration’.

Example 3: Checking whether an object is iterable or not.

Difference in Iterable and Iterator in Python for Data Science - PST Analytic

To learn more about python for data science, you can go to this and this. These blogs can help you in preparing for a data scientist. So, if you want to be a data scientist in future, you need to start early and start learing some skills.

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.