Iterable in Python is any object that can be looped over. In order for an iterable to be looped over, an iterable needs to be converted to an iterator using the 7 method. Once created, it is actually the iterator that gets iterated over. This is also what happens internally when you run a for-loop. Show Let’s understand iterators and iterables very clearly. This covers: There is a minor difference between an iterable and an iterator. For example, the List is an iterable but not an iterator. Let’s understand the difference clearly, so you can write python code that is more efficient, and will enable you to see solutions to problems in a way you might not have thought through before. Many of the python objects that we have seen so far are ‘Iterables’. List, string, tuples etc are iterables. What is an iterable?An iterable is basically a Python object that can be looped over. This means, lists, strings, tuples, dicts and every other object that can be looped over is an iterable. See this for-loop for example.
Output
So what really happens when you run a for loop? An iterable defines an 7 method which returns an iterator. This means, everytime you call the 9 on an iterable, it returns an iterator.
The iterator in turn has 0 method defined.So, whenever you use a for-loop in Python, the 0 method is called automatically to get each item from the iterator, thus going through the process of iteration.In a similar way, you can loop over strings, tuples, dictionaries, files, generators (which we will cover next) etc. How to tell if an object can be looped over or is an iterable?You can tell whether an object is iterable or not by the presence of the 7 dunder method.MLPlus Industry Data Scientist ProgramDo you want to learn Data Science from experienced Data Scientists? Build your data science career with a globally recognised, industry-approved qualification. Solve projects with real company data and become a certified Data Scientist in less than 12 months. . Get Free Complete Python CourseBuild your data science career with a globally recognised, industry-approved qualification. Get the mindset, the confidence and the skills that make Data Scientist so valuable. So, technically speaking, any python object that defines a 7 method, is an iterable. This is a special method, aka, a ‘Dunder method’ or ‘magic method.’
Output:
You can find the 4 method in the list above. Likewise, you can find that method in every other iterable.We are now clear with what an iterable is. So, what is an iterator? What is an iterator?You know that you will get an 5 by calling the 9 on an iterable.Iterator is an iterable that remembers its state. Which means, it’s a python object with a state so it remembers where it is during iteration. Like how an iterable has a 7 method, which gives an iterator, an iterator defines a 0 method that makes the iteration possible.
Output:
Output
We now have an iterator. It must have a state, so the next time it is iterated, it will know how to get the next value. It does it using the dunder 0 method.So, technically, an iterator is an object that has executed the dunder methods: 4 and 1. 0Output 1 2Output 3When you call 2, it is actually calling the dunder method 0 in the background. 4Output 5After it has exhasted all the items, it errors our with 4 on further calling 0. 6That means the iterator has been exhausted. Also notice, the list 6 also contains the 7 method, which makes it return the same iterable, instead.Creating your own iterator objectPython allows you to create your own iterator object. All you need to do is to define the 7 and 0 methods, along with the constructor ( 0) ofcourse. 7Output: 8 9 0 1Output: 2 3Practice Exercises on Iterators:Q1: Make changes to the code so that it returns a list of words in each line. Q2: Write an iterator class that reverses a string Solution 2: 4 5Output: 6Awesome! That’s how you create an iterator on your own. Now, if this feels cumbersome, you can create and work with Generators. Selva PrabhakaranSelva is the Chief Author and Editor of Machine Learning Plus, with 4 Million+ readership. He has authored courses and books with100K+ students, and is the Principal Data Scientist of a global firm. |