Python is one of the most popular programming language by its user-friendly and clear, concise syntax structure that 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.
Iterable and Iterator are the Python Advanced Concept that most Python programmers use this concept knowingly or unknowingly in their program.
- Iterable is an object(containers for data) which 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 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()
- Iterator object can be used to iterate over an iterable object using 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 at the fifth time because iterators will always remember the last item that they returned. Thus, we get the error when we try to call 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))
As 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 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 that iterator object in for loop does not return any exception because loop runs exactly 4 times.
What if we call next() method after loop end as:
...... ...... # convert to Iterator my_list_iter = iter(my_list) for num in my_list_iter: print(num) next(my_list_iter)
Above program raise StopIteration Exception as their is no any further element left after for loop finished. You can check it in your environment.
Therefore, the iterator object can be use only once as the for loop has already made the iterator iterate through all elements. There remains no any item in object causing exception.
But in the case of 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 next() method and see 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 do not support 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 object by Iterable.
If you want to know the Python Tips and Tricks, then follow this link.