An iterator is an object that contains a countable number of values.
Technically, in Python, an iterator is an object which implements the iterator protocol, which consists of the methods __iter__() and __next__().
Lists, tuples, dictionaries, and sets are all iterable objects. They are iterable containers which you can get an iterator from. All these objects have an iter() method which is used to get an iterator.
A python generator function lends us a sequence of values to python iterate on.
We used a function to create generator. But in creating an iterator in python, we used the iter() and next() functions.
A generator in python makes use of the ‘yield’ keyword. A python iterator doesn’t.
Python generator saves the states of the local variables every time ‘yield’ pauses the loop in python.
An iterator does not make use of local variables, all it needs is iterable to iterate on.
A generator may have any number of ‘yield’ statements.
You can implement your own iterator using a python class; a generator does not need a class in python.
To write a python generator, you can either use a Python Function or a comprehension. But for an iterator, you must use the iter() and next() functions.
Generator is efficient for writing fast and compact code. This is an advantage over Python iterators. They are also simpler to code than do custom iterator.
Python iterator is more memory-efficient.
In questa pagina del sito puoi guardare il video online Iterator, Iterables and Generator - Python Tutorial in Tamil della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Surendar Manoj 29 January 2022, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 2,999 volte e gli è piaciuto 70 spettatori. Buona visione!