Using iterators and generators for data processing
Iteration is one of the key tools used for data processing and data transformation. Iterations are especially useful when dealing with large datasets and when bringing the whole dataset into memory is not possible or efficient. Iterators provide a way to bring the data into memory one item at a time.
Iterators can be created by defining them with a separate class and implementing special methods such as __iter__
and __next__
. But there is also a new way to create iterators using the yield
operation, known as generators. In the next subsections, we will study both iterators and generators.
Iterators
Iterators are the objects that are used to iterate on other objects. An object on which an iterator can iterate is called iterable. In theory, the two objects are different, but it is possible to implement an iterator within the iterable
object class. This is not recommended but is technically possible and we will discuss with...