why we use iterators when we have for loops to iterate on anything which is iterable???
Iterators doubt
In Python, an iterator is an object which implements the iterator protocol. The iterator protocol consists of two methods. The iter() method, which must return the iterator object, and the next() method, which returns the next element from a sequence
Iterators have several advantages:
- Cleaner code
- Iterators can work with infinite sequences
- Iterators save resources
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.