Skip to main content

3 docs tagged with "iterators"

View all tags

Generators: Creating Iterators with `yield`

In the last article, we learned how to create a custom iterator by building a class that implements the full iterator protocol (iter and next). While this is powerful, it's also quite verbose for many common use cases.

Iterators and Iterables: The Iterator Protocol

One of the most powerful and frequently used features in Python is the for loop. We use it to loop over lists, strings, dictionaries, and files. But how does it actually work? How does the for loop know how to handle all these different types of objects?