Hey guys,
welcome to Free Tutorial TV and Python tutorial for beginner.
And in this video 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, Tupple, 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 what Lets Take an Example,
That, I'm going to declare a collection which is List.
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 iterate all would 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 a collection.
Now this iterator object has two special methods.
First is underscore underscore I T E R underscore underscore.
And second is underscore underscore next
Underscore underscore.
So iterators have these two special Methods one is ITER . and this Method is going to get you the iterator.
and then the next Method is going to give you the next value using this iterator.
Now collections like lists or dictionary or tupple are all iterable collections.
That means we can iterate over them. and all these collection objects have our special Method called
iter. which is used to get that iterator from these collections.
So let me show you.
First of all these two Methods inside our collection.
So when you use D I R, inbuilt function. and give any collection as an argument here . 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 I t E r .
So all these collections like lists or dictionary or tupple have this I T E R method .and we can call
this ITER method to get the object of the iterator . and we can call this underscore underscore I T E R method using our 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 our special Method called I T E R without underscore.
And this iter Method is used to get that 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 underscored underscore iter underscore underscores. 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 that 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 The next here. to get the first value of this collection.
So I'm going to just pass I T, which is the iterator which we have got from this.
Iter Method.
**************************************************************************
please like and follow us in Facebook and Instagram.
facebook: / technologytv
Instagram: / heikhamsingh
Twitter: / hanandkumarsing