Iterator Blog Posts

Python Iterator and creating our own iterators

In this article we are going to know more about Python iterators and how to create our own iterators in Python.
What is an iterator
An iterator is an object that produces a series of values for iteration. More precisely if i is an iterator then for each call to next(i) will produces an element and raise StopIteration exception when all series elements are iterated.
So lets see the below example
n_list = [1,2,3,4,5]
n_iterator = iter(n_list)
print(next(n_iterator))

0 Likes 1012 Views