Skip to content

Python Generator vs Iterator

  • by

Generators and iterators are related concepts in Python that both involve generating sequences of values, but they serve slightly different purposes and have different implementations. Let’s explore each of them:

Let’s compare the syntax of Python generators and iterators side by side:

Iterator Syntax:

# Iterator Class
class MyIterator:
    def __init__(self, limit):
        self.limit = limit
        self.current = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.current < self.limit:
            value = self.current
            self.current += 1
            return value
        else:
            raise StopIteration

# Using the Iterator
my_iter = MyIterator(5)
for num in my_iter:
    print(num)
  • Requires defining a class with __iter__() and __next__() methods.
  • Uses the raise StopIteration statement to indicate the end of iteration.
  • The class-based approach is more verbose.

Generator Syntax:

# Generator Function
def my_generator(limit):
    current = 0
    while current < limit:
        yield current
        current += 1

# Using the Generator
gen = my_generator(5)
for num in gen:
    print(num)
  • Uses a regular function with the yield keyword to define the generator.
  • The yield keyword automatically handles the suspension and resumption of the function.
  • The generator function is typically shorter and more straightforward.

Here’s a tabular comparison between Python generators and iterators:

AspectIteratorsGenerators
ImplementationRequires implementing __iter__() and __next__() methods in a class.Uses a special function with the yield keyword.
Memory EfficiencyMay require storing the entire sequence in memory, which can be memory-intensive for large datasets.Generates values on-the-fly, consuming less memory.
State MaintenanceRequires managing the iterator’s state manually in the class.Maintains its own state automatically between yield statements.
Ease of UseInvolves writing and managing a class, which can be more complex.Uses a simpler function with yield, often easier to understand.
Lazy EvaluationValues are retrieved when requested, but computed in advance.Values are generated as needed, supporting true lazy evaluation.
Use CasesSuited for custom data sources and complex traversal logic.Ideal for simple sequence generation and lazy evaluation.

Python Generator vs Iterator example

Simple example code of a Python iterator and a Python generator that both produce the sequence of even numbers up to a given limit:

Iterator Example:

class EvenNumbersIterator:
    def __init__(self, limit):
        self.limit = limit
        self.current = 0

    def __iter__(self):
        return self

    def __next__(self):
        while self.current < self.limit:
            value = self.current
            self.current += 2
            return value
        raise StopIteration

# Using the Iterator
even_iter = EvenNumbersIterator(10)
for num in even_iter:
    print(num)

Generator Example:

def even_numbers_generator(limit):
    current = 0
    while current < limit:
        yield current
        current += 2

# Using the Generator
even_gen = even_numbers_generator(10)
for num in even_gen:
    print(num)

Output:

Python Generator vs Iterator

Both the iterator and the generator produce the same output, which is a sequence of even numbers up to the limit of 10. However, the generator example is more concise and easier to understand. It uses the yield keyword to emit each value in the sequence, and the generator function’s state is automatically maintained between calls.

In the iterator example, we need to explicitly manage the iterator’s state and handle the StopIteration exception to indicate the end of the sequence.

Generators are generally preferred for their simplicity, memory efficiency, and more natural way of expressing lazy evaluation and sequence generation.

In summary, the generator syntax is generally simpler and more concise compared to the iterator syntax. Generators provide a more intuitive way to create sequences of values, especially for scenarios that involve simple sequence generation and lazy evaluation.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *