Efficient Iterations With Python Iterators and Iterables (Overview)
Python’s iterators and iterables are two different but related tools that come in handy when you need to iterate over a data stream or container. Iterators power and control the iteration process, while iterables typically hold data that you want to iterate over one value at a time.
Iterators and iterables are fundamental components of Python programming, and you’ll have to deal with them in almost all your programs. Learning how they work and how to create them is key for you as a Python developer.
In this video course, you’ll learn how to:
- Create iterators using the iterator protocol in Python
- Understand the differences between iterators and iterables
- Work with iterators and iterables in your Python code
- Use generator functions and the
yield
statement to create generator iterators - Build your own iterables using different techniques, such as the iterable protocol
00:00
Welcome to Efficient Iterations With Python Iterators and Iterables. My name is Christopher and I’ll be your guide. In this course, you’ll learn about iterables, the iter()
, and next()
built-in functions, iterators and the iterator protocol, generators, and the yield
keyword, generator expressions, and the sequence protocol.
00:26 The code in this course was tested using Python 3.12, but any supported version of Python from 3.7 up will work.
00:34
Sometimes it can be fun to drill down on something that you’ve taken for granted. For example, have you ever considered just how a for
loop actually works?
00:42
What kinds of things can you iterate over? What kinds of things can go after the in
keyword and how does the loop get the next item during the actual iteration?
00:53
Sure, there are loads of built-in things you can iterate on, but can you write your own? Well, the answer to that last one is yes. Iteration in Python is based on the iterator protocol, which is a loose definition of class methods that get used by the for
loop and other code that iterates.
01:11 This course covers just what it means to be an iterable, how iterators are related to iterables, and how you can write your own code that implements the iterator protocol.
01:22 Next up, I’ll dive into what it means to be an iterable.
Become a Member to join the conversation.