Python Generators vs Iterators
Generators and Iterators are Python constructs that lets you loop through data in chunks instead of loading them into memory. This post discusses the subtle differences between them
In Python, both generators and iterators ensure data is not loaded into memory on the whole but rather processed chunk by chunk. But when to use a generator and iterator.
What is a Generator?
A generator is a Python function that yeild
a result. Every generator is an iterator. A generator function returns a generator object
What is an Iterator?
An iterator is a Python object which returns an iterable via __iter__
method. An iterable has __next__
Python Iterators and Iterables talks about iterators in detail, but for now, let's look at a simple example
When to use a Generator vs. an Iterator?
Generators are function, and Iterators are object-oriented.
Use generators when you have a large stream of data and you want to loop over them.
Use iterators to generate a sequence of data.
Generators are excellent for large loops since it only works on one value at a time.
Last updated