Iterator, Iterable and Iteration Explained with Examples

Python : Iterator, Iterable and Iteration Explained with Examples

Iterator , Iterable and Iteration with Examples

1)Iterable

Iterable is a type of object that is made up of other components. Iterables include things like list and tuple. It’s identical to any Java collection class or C++ container class. When the iter() function on an Iterable object is called in Python, an Iterator is returned, which can be used to iterate over the elements inside Iterable.

If a class has overloaded the magic method __iter__() in Python, it is called Iterable. The Iterator object must be returned by this feature.

2)Iterator

In Python, an iterator is an entity that iterates over iterable objects such as lists, tuples, dicts, and sets. The iter() method is used to create the iterator item. It iterates using the next() form.

The iterator’s initialization is handled by the __iter(iterable)__ process. Next ( __next__ in Python 3) returns an iterator item. The iterable’s next value is returned by the next process. When we use a for loop to traverse some iterable object, it uses the iter() method to obtain an iterator object, which we then iterate over using the next() method. To signal the end of the iteration, this method raises a StopIteration.

3)Iteration

Iteration is the method of looping through all of the elements of an Iterable with the help of an Iterator object. We can iterate in Python using for loops or while loops.

4)Traverse/Iterate over list (Iterable) using Iterator

Since the __iter__() feature, which returns an Iterator, has been overloaded in Python, List is an Iterable item. The iter() function is used to get the Iterator object from an Iterable object. From the list, let’s get the Iterator object.

Since the __next__() function is overloaded, it returns a list iterator object, which is essentially an Iterator. You may also look at the Iterator form that was returned.
Let’s use this iterator object to loop through the contents of the list.

Below is the implementation:

# given list
givenlist = ["hello", "this", "is", "BTechGeeks"]
# getting the iterator of the list
itrlist = iter(givenlist)
# Traverse the list using iterator
while True:
    try:
        # getting next element in the list using next()
        element = next(itrlist)
        print(element)
    except StopIteration:
        break

Output:

hello
this
is
BTechGeeks

Explanation:

Call the next() function on the Iterable’s iterator object to get the next value. It will return the Iterable’s next value. To get all elements of iterable one by one, keep calling this next() function. When the iterator reaches the end of the iterable’s components, the StopIteration error is thrown.

As previously said, the next() function is similar to Python’s hasnext() function. It is used to get the Iterable’s next value.

5)Iterate over an Iterable (list) using for loop

Python has a fantastic for loop that iterates over the Iterable object using Iterator. For example, we can use the for loop to iterate over the elements in a list (Iterable).
Internally, the for loop does the same thing, retrieving the Iterator from Iterable and using it to iterate over the elements in Iterable. However, it offers a short hand form; we don’t need to write a large while loop or catch StopIteration errors ourselves because it handles everything internally.
We may iterate over other Iterables as well, such as list, tuple, and custom Iterators.

Below is the implementation:

# given list
givenlist = ["hello", "this", "is", "BTechGeeks"]
# traverse the list using for loop
for element in givenlist:
    print(element)

Output:

hello
this
is
BTechGeeks

6)Python Iter() function

When the iter() function is called, the passed object is passed to the __iter__() function, which returns the value received by it. Iterable refers to a class that has an overloaded __iter__() function and is supposed to return an Iterator object.

7)Python next() function

When the next() function is called, the passed object is passed to the __next__() function, which returns the value returned by it. Iterator objects, in essence, overload the __next__() function, returning the next value from the corresponding Iterable item.
Related Programs: