Python is one of the most popular programming languages by its user-friendly and clear, concise syntax structure, and the layman also clearly understood the flow of the program. It supports a wide range of functionality that could be used from Web Development to big Data Science projects.
Python Iterable and Iterator are the Advanced concepts that most Python programmers use this concept knowingly or unknowingly in their programs.
- Iterable is an object(containers for data) that you can iterate over with the help of a for loop.
- Objects in iterable can be lists, sets, tuples, strings, dictionaries, etc.
- Iterable has a built-in magic/dunder method __iter__()
- The iterable object when passed to the iter() method generates an Iterator.
- An iterator is an object that contains items that can be iterated upon, or traverse through all values one by one until no item is left.
- Iterator supports two built-in magic methods: __iter__() and __next()
- An iterator object can be used to iterate over an iterable object using the next() method.
Creating a Simple Iterator
Let’s see the example in which we create iterators from iterables using the iter() function as:
# iterable object country = ["USA", "India", "UK", "Australia"] # convert iterable to iterator using iter() iter_country = iter(country) # print first item print(next(iter_country)) # print second item print(next(iter_country)) # print third item print(next(iter_country)) # print fourth item print(next(iter_country)) # Iterator object is exhausted # as all item are returned # raise StopIteration Exception print(next(iter_country))
USA India UK Australia StopIteration --------------------------------------------------------------------------- StopIteration Traceback (most recent call last) /tmp/ipykernel_33844/1675550588.py in <module> 20 # as all item are returned 21 # raise StopIteration Exception ---> 22 print(next(iter_country)) StopIteration:
In the above output, we get StopIteration Exception only after the calling next() function for the fifth time because iterators will always remember the last item that they returned. Thus, we get the error when we try to call the next() method, after all the items in the list were returned.
Differences between Iterable and Iterator
|Supports only one built-in function: iter()||Supports two built-in functions: iter() and next()|
|Iterable objects can be iterated using for loop||Can also be iterated using for loop|
|Iterables are not iterators||Iterators are iterables|
Iterable & Iterator Built-in functions
Let’s check the iter() function available in the iterable object.
# iterable object: list my_list = [1, 2, 3, 4] # print all the functions # supported by this object print(dir(my_list))
From the above output, we can notice that __iter__() present but not __next__() function which also tells that Iterables are not iterators.
Let’s check the iter() and next() functions available in the iterator object.
# iterable object: list my_list = [1, 2, 3, 4] # convert to Iterator my_list_iter = iter(my_list) # print all the functions # supported by this object print(dir(my_list_iter))
An iterator object contains both
__iter__() and __next_() functions, we say that all Iterator objects are also iterables.
Iterators are iterables
As the iterable object can be iterated using for loop, we can also iterate over the iterator object using for loop.
# iterable object: list my_list = [1, 2, 3, 4] print("Iterable object") for num in my_list: print(num) print("-----") print("Iterator object") # convert to Iterator my_list_iter = iter(my_list) for num in my_list_iter: print(num)
Iterable object 1 2 3 4 ----- Iterator object 1 2 3 4
Above example proves that we can use for loop in both iterable and iterator objects.
But one thing, you may notice is that the iterator object in for loop does not return any exception because the loop runs exactly 4 times.
What if we call the next() method after the loop end as:
...... ...... # convert to Iterator my_list_iter = iter(my_list) for num in my_list_iter: print(num) next(my_list_iter)
The above program raises StopIteration Exception as there is no further element left after for loop is finished. You can check it in your environment.
Therefore, the iterator object can be used only once as the for loop has already made the iterator iterate through all elements. There remains no item in the object causing exception.
But in the case of an iterable object list, we can iterate over and over again using for loop.
Iterables are not iterators
Let’s take iterable objects and try to implement the next() method and see the output.
# iterable object: string my_country = "Nepal" my_country.__next__() # iterable object: list my_num = [1, 2, 3, 4] my_num.__next__()
...... AttributeError: 'str' object has no attribute '__next__' ....... AttributeError: 'list' object has no attribute '__next__'
As the iterable object does not support the next() method, we can say that iterable objects are not iterators.
Hence. we come to conclude that there are huge differences between Iterable and Iterator and we should use them wisely in our program.
While processing huge sets of data, we should use Iterator as it only holds the memory size to return value instead of reserving the memory for an object by Iterable.
If you want to know the Python Tips and Tricks, then follow this link.