Iterator and Generator
Iterators and generators are powerful features in Python that allow you to work with sequences of data efficiently. In this tutorial, we will explore iterators and generators, how they work, and when
Iterators
An iterator is an object that represents a stream of data. You can iterate (loop) over the elements of an iterator one at a time. Python provides two essential methods for working with iterators: __iter__()
and __next__()
.
Creating an Iterator:
To create an iterator, you need to define a class with two methods: `__iter__()`
and `__next__()`
. The `__iter__()
` method returns the iterator object itself, and the `__next__()`
method returns the next value from the iterator.
2. Using an Iterator:
To use an iterator, you can create an instance of the iterator class and use a `for`
loop to iterate over its elements.
This will print the numbers 1 through 5.
Generators
Generators provide a more concise and convenient way to create iterators. They are defined as functions using the `yield` keyword. When a function contains one or more `yield` statements, it becomes a generator. Generator functions do not execute immediately; they return a generator object that can be iterated over.
1. Creating a Generator:
Here's how you can define a generator function:
2. Using a Generator:
To use a generator, you can call the generator function, which returns a generator object. You can then iterate over the values it yields using a `for` loop.
This will also print the numbers 1 through 5.
Generator Expressions
Generator expressions provide a concise way to create generators. They are similar to list comprehensions but use parentheses instead of square brackets.
This code will produce the same result as the previous examples.
When to Use Iterators and Generators
Use iterators when you need to create a custom iterable object with more complex logic.
Use generators when you want a simpler way to create iterable sequences, especially for large data sets, as they are memory-efficient.
Generator expressions are handy for creating simple generators on the fly.
Last updated