What is a Python generator, and how does it differ from an iterator?

This article explains the concepts of Python generators and iterators, highlighting their differences and illustrating their importance in memory-efficient programming. …

Updated August 26, 2023



This article explains the concepts of Python generators and iterators, highlighting their differences and illustrating their importance in memory-efficient programming.

Understanding the difference between generators and iterators is crucial for mastering Python’s powerful features and writing efficient code.

Let’s break down these concepts step by step.

What are Iterators?

Imagine you have a list of numbers. An iterator is like a pointer that traverses this list, one element at a time. You can use the next() function on an iterator to get the next element in the sequence. Once you reach the end, calling next() will raise a StopIteration exception.

Code Example:

my_list = [1, 2, 3, 4]
my_iterator = iter(my_list)  # Create an iterator from the list

print(next(my_iterator))  # Output: 1
print(next(my_iterator))  # Output: 2
print(next(my_iterator))  # Output: 3
print(next(my_iterator))  # Output: 4
# print(next(my_iterator)) # This would raise a StopIteration exception

Key Points about Iterators:

  • They have a __iter__() method, which returns the iterator object itself.
  • They have a __next__() method, which returns the next element in the sequence.

What are Generators?

Generators are special functions that use the yield keyword instead of return. When a generator function is called, it doesn’t execute the entire code block immediately. Instead, it pauses execution every time it encounters yield, returning a value to the caller. The next time the generator is called (using next()), it resumes execution from where it left off.

Code Example:

def my_generator(n):
    for i in range(n):
        yield i * 2

my_gen = my_generator(5)

print(next(my_gen))  # Output: 0
print(next(my_gen))  # Output: 2
print(next(my_gen))  # Output: 4

Key Points about Generators:

  • They are defined using functions with the yield keyword.
  • They generate values on demand, making them memory-efficient for large datasets.

The Key Differences

| Feature | Iterator | Generator | |—–||—-| | Creation | Created explicitly using iter() | Created implicitly using function with yield | | Execution | Executes code block at once | Executes code in chunks, pausing at yield | | Memory Usage | Stores the entire sequence | Generates values on demand, saving memory |

Why is this Important for Learning Python?

Understanding generators and iterators is crucial because they:

  • Allow you to work with large datasets efficiently without loading everything into memory.

  • Enable elegant and concise code solutions for tasks like processing data streams or infinite sequences.

  • Showcase the power and flexibility of Python’s language design.

Let me know if you have any other questions about generators, iterators, or any other Python concepts!


Stay up to date on the latest in Computer Vision and AI

Intuit Mailchimp