Python Iterator

Python Iterators

An iterator is an object that contains a countable number of values.

An iterator is an object that can be iterated upon, meaning that you can traverse through all the values.

Technically, in Python, an iterator is an object which implements the iterator protocol, which consist of the methods __iter__() and __next__().

Iterator vs Iterable

Lists, tuples, dictionaries, and sets are all iterable objects. They are iterable containers which you can get an iterator from.

All these objects have a iter() method which is used to get an iterator:

Example

Return an iterator from a tuple, and print each value:

 
mytuple = ("apple", "banana", "cherry")
myit = iter(mytuple)


    print(next(myit))
print(next(myit))
print(next(myit))

Even strings are iterable objects, and can return an iterator:

Example

Strings are also iterable objects, containing a sequence of characters:

 
mystr = "banana"
myit = iter(mystr)


    print(next(myit))
print(next(myit))
print(next(myit))

    print(next(myit))
print(next(myit))
print(next(myit))

Looping Through an Iterator

We can also use a for loop to iterate through an iterable object:

Example

Iterate the values of a tuple:

 
mytuple = ("apple", "banana", "cherry")


for x in mytuple:

      print(x)

Iterators in Python

Iterator in python is an object that is used to iterate over iterable objects like lists, tuples, dicts, and sets. The iterator object is initialized using the iter() method. It uses the next() method for iteration. 

How an iterator really works in python 

 # Here is an example of a python inbuilt iterator
# value can be anything which can be iterate
iterable_value ='Geeks'
iterable_obj =iter(iterable_value)
 
whileTrue:
    try:
 
        # Iterate by calling next
        item =next(iterable_obj)
        print(item)
    exceptStopIteration:
 
        # exception will happen when iteration will over
        break

Output : 

 G                                                                                                                                                                            
e                                                                                                                                                                            
e                                                                                                                                                                            
k                                                                                                                                                                            
s


For more, checkout W3Schools and Geeks For Geeks.