Python Tutorial for Beginners 41 - Python Iterators



 hey guys welcome to the next blog on python tutorial for beginners in this blog I'm going to show you what our iterators in Python and how to use iterators in Python and at the end I'm also going to show you how you can create your custom iterator class in Python so let's get started and let's see first of all what is an iterator but before that we need to understand what is iteration so what is iteration an act of going over a collection is called iteration now what kind of collections we are talking about we have already seen some kind of collections like lists tuples dictionaries sets and other kind of collections and till now what we have seen is we can use a for loop to iterate over a collection so till now what we have learned is for example I am going to declare a collection which is lists in this case and I'm going to just add some values inside the list and once I declare the list I can iterate over this list using a for loop so let's say for I in the collection name and then you can just write print here to print each and every value of this collection a so this for loop iterates over each and every element in the collection and then print the value using this print statement now let's talk about the iterators so what is an iterator iterator is an object which can be used to iterate over our collection now this iterator object has two special methods first is underscore underscore ITER underscore underscore and second is underscore underscore next underscore underscore so iterators have these two special methods one is eater and this eater method is going to get you the iterator and the next method is going to give you the next when you using this iterator now collections like lists or dictionary or tuples are all iterate able collections that means we can iterate over them and all these collection objects have a special method called eater which is used to get the iterator from these collections so let me show you first of all these two methods inside our collection so when you use dir inbuilt function and give any collection as an argument here and when I press Enter you will see a list of functions here and when I scroll a little bit right here you will see a special function which is ITER so all these collections like lists or dictionary or tuple have this ITER method and we can call this IDR method to get the object of the iterator and we can call this underscore underscore ITER method using a special method called ITER without underscore in front and back of it so let me just show you what I'm saying so I'm going to declare a variable I T which stands for iterator and then I'm going to use a special method called ITER without underscore and this ITER method is used to get the iterator so I can just pass my collection here as an argument and this iterator method is going to give me the iterator object of this collection which I was talking about which is underscore underscore eater underscore underscore so let me just press ENTER and now we have the iterator object and this iterator object we can use to call all the values in the collection so let's once again print the values of our A List and this has these six values and now I can use a special method called Next here to get the first value of this collection so I'm going to just pass the ite which is the iterator which we have got from this eater method and then press ENTER and you will see it will print the first value of your list now once again when you call this next using this ite variable is going to give you the next value of the list and when you call it again and again it's going to iterate over your list and give you the values of the list one by one so let's get all the values so this is the second last value which is nine and then at last it's going to print the last value which is seven now at this point iterator has iterated over all these six values after that when you call this next method once again over this IT variable then it's going to give you this exception which says stop iteration so when your iterator is exhausted then it's going to give you the exception which is stop iteration now as I said the collections like list dictionary or tuple have this iterate able object that means we can use the for loop to iterate over these collections so for loop uses the same iterator mechanism to iterate over all these elements of the collection now many a times in your programming development career you will have to write a class in order to iterate over some kind of collection so let me show you how you can create your custom iterator class so let me minimize this Python console in the PyCharm IDE and I will just comment these two methods so as I said an iterator always have these two special methods one is underscore underscore eater underscore underscore and the second is underscore underscore next underscore underscore so in order to create your own iterator class you need to implement both these methods so let's create an iterator class and I'm going to just name my class as list iterator class and in this list iterator class first of all I'm going to define init method and this init method is going to take any type of list or any type of collection which is iterate table and then inside this init method I'm going to just call a self to create member variable here and I'm going to name my member variable as dot underscore underscore list and I'm going to just pass the value of a let's name this a as list also so we will be clear so list is equal to list the second member variable which I want to create here is the index variable so the second variable is the index variable which is the index of the list or the collection and let's give the initial value of this index now as I said in order to create an iterator class you need to implement these two methods which are underscore underscore ITER method first of all and in this method we are just going to return the value of self so this underscore underscore ITER method is going to give you the iterator and let's also implement the method which is underscore underscore next now in this next method first of all when this next method is called what we want to do is we want to increase the value of index so let's call our index member variable and let's increase the value of index by one and the next thing which we want to do here is we want to send the value at this index so we can just return so let me call return first of all and then self dot your list name whatever is your list and then inside these square brackets we call the index which we have incremented by one so self dot underscore underscore index so this statement is going to give you the current value at this index right now and that's basically it so I'm going to just say my list is equal to list iterator and this list iterator takes an argument which is a collection right so let me create a list collection and I'm going to name my list as a and here I'm going to provide some values inside the list so let's say we have these six values inside the list and now we can just pass this list as an argument of our list iterator so once we have this list iterator object we can call ITER method in order to get the iterator object which is this one so let me create one more variable which is I T and then I'm going to call this ITER method and I'm going to pass my my list object as an argument of this ITER method so now I have the iterator object of this list using this list iterator class and now I can use the print method so let me call a print method and then I can call the next method to iterate over the list using the ID variable so let's run this code once again and you will see it's going to print the first value of the list which is one let's print it two more times and let's run the code once again and you can see it's going to give you the next two values of the list so now we know that this next is going to call every time the next value from this list so let's print all the values of the list using this next method and you can see it prints all the values of this list now when I want to go beyond this after the list iterator is exhausted and let's run the code you will see it's going to give me this error which says list index out of range but this is not the problem of the iterator is the problem of the list now what I have shown you when I have shown you the example of this iterator in the Python console whenever your iterator is exhausted it throws the stop iteration error or exception so let's raise the same kind of stop iteration exception using our class also so I will go to the next method of my class and here I can give a condition and this condition checks whether the value of index so self dot underscore underscore index is greater than or equal to the length of your list so length and the list name is self dot underscore underscore list and when this condition is true we are going to raise an exception so let's use the raised keyword and the exception which we want to raise here is the top iteration exception so let's throw this exception or raise this exception and what this condition is basically going to do is until the last index which is for example in this list the last index is 5 so on this until the last index everything will be ok but as soon as this index becomes 6 which is the length of your list also so the length of the list which have 6 element is 6 but index will go up to 5 because the index starts from 0 so as soon as this index becomes is equal to the length which means the iterator has been exhausted we can also write double equals to here as soon as the value of index becomes the length of the list we can throw this exception so let's run this code once again and you can see we have used this next method seven times so at the last print statement is going to give us the error so first of all let me remove the last statement and let's run the code is going to run fine so you can see everything is fine but as soon as I call this last next method and I know that my iterator is exhausted and when I run the code once again it's going to raise an exception which is iteration now one more thing which I said which you can use with your iterators is the for loop so let's try to use this for loop and I'm going to just write for I in your iterator which is I T which we have got using this ITER method and let me comment this code also and now I can print each and every element inside the list a so I'm going to just write print I here and let me remove this code from here so you can see the list also and this for loop also and I'm going to run this code once again and you can see this for loop has iterated over our list which we have provided as an argument of this list iterator class and you can see all the values inside the list have been printed using this for loop so in this way you can create your own iterator class in Python I hope you have enjoyed this blog and I will see you in the next blog you 

Post a Comment

Previous Post Next Post

Recent in Technology News