Python Iterator With Examples

Good day learners! In our previous tutorial, we learned about Python Operator Overloading. In this tutorial we are going to learn about Python Iterator.

Python Iterator

Knowingly or unknowingly, you have used iterator in your Python code. Basically, an iterator is an object which is used to iterate through an iterable element. Didn’t understand the meaning of the last line? Well, don’t worry. We will make you understand about Python Iterator through our tutorial.

Python Iterator and Iterable Elements

Most of the objects of Python are iterable. In python, all the sequences like Python String, Python List, Python Dictionary etc are iterable. Now, what is iterator? Suppose, A group of 5 boys are standing in a line. Your are pointing at the first boy and ask him about his name. Then, he replied. After that, you ask the next boy and so on. The below picture will illustrate the thing.

Python Iterator

In this case, you are the Iterator!!!! Obviously, the group of boys is the iterable element. Hope you understand now.

Python Iterator Protocol

Python Iterator Protocol includes two functions. One is iter() and the other is next(). In this section, we will learn to how to traverse an iterable element using Python Iterator Protocol.

In the previous section, we gave the example of group of 5 boys and you. You are the iterator and the boys group is the iterable element. After knowing the name of one boy, you ask the same question to the next boy.

After that, you do it again. iter() function is used to create an iterator of an iterable element. And the next() function is used to iterate to the next element.

Python Iterator Example

If the iterator go beyond the number of iterable elements then the next() method will raise StopIteration exception. See the code below for python iterator example.

Output

Making of a Python Iterator

However, you can make your own specified Python Iterators. To do so, you have to implement a Python class. I assume that you know about Python Class. If you don’t know about this, you can read our tutorial about Python Class.

As we have said earlier that, Python Iterator Protocol consist of two methods. So we need to implement those method.

For example, you want to generate a list of fibonacci number so that each time call the next function it returns you the next fibonacci number.

To raise the exception, we limit the value of n below 10. If the value of n reach 10, it will raise an exception. The code will be like this.

So, the output will be

Why Making Python Iterator

After going through the previous section, a question may arise to your mind that why should we make Python Iterator.

Well, we have seen already that iterator can traverse an iterable element. Suppose, in our previous example if we make a list of fibonacci numbers and then traverse it via a Python Iterator, it would take huge memory. But if you create a simple Python Iterator class, you can serve your purpose without consuming that much memory.

So that’s all for Python Iterator. Hope that you are now able to work with Python Iterator. For any further query, you can use the comment box. Learn and practice as much as you can.

#HappyCoding

Reference: Python Doc

By admin

Leave a Reply

%d bloggers like this: