1. What Are Iterators?
An iterator is an object that can be iterated (looped) over. It uses __iter__()
to return itself and __next__()
to move through elements.
Example:
numbers = [1, 2, 3]
iterator = iter(numbers)
print(next(iterator)) # Outputs: 1
Iterators handle sequences efficiently, especially when working with large datasets.
2. Generators: A Lightweight Way to Create Iterators
Generators produce items one at a time and are defined using functions with yield
instead of return
. This keeps memory usage low and makes them ideal for processing large data.
Example:
def countdown(num):
while num > 0:
yield num
num -= 1
for i in countdown(3):
print(i) # Outputs: 3, 2, 1
The yield
statement saves the function’s state, allowing it to pick up where it left off each time it’s called.
3. Why Use Generators?
Generators are excellent for:
- Handling large datasets without loading everything into memory.
- Lazy evaluation, producing items only as needed.
4. Generator Expressions
Generator expressions provide a compact syntax for generators, similar to list comprehensions but with parentheses.
Example:
squares = (x * x for x in range(5))
print(next(squares)) # Outputs: 0
Closing Thoughts:
With generators, Python handles data streams smoothly without memory overhead, making your code efficient and scalable.
Top comments (1)
Generators are awesome!