Chapter 11: Iterators and Generators
Iterators
# Create an iterator
numbers = iter([1, 2, 3, 4, 5])
# Access elements using next()
print(next(numbers)) # Output: 1
print(next(numbers)) # Output: 2
# Iterate over the rest using a loop
for num in numbers:
print(num) # Output: 3, 4, 5Custom Iterators
Generators
Generator Expressions
Advantages of Generators
Practical Use Cases
Exercises
Best Practices
Last updated