Getting Started
00:00 In the previous lesson, I gave an overview of the course. In this lesson, I’ll introduce you to the concept of interables and how they interact with iterators. In English, to iterate is to say or do something again, or again and again.
00:17 Repetition. See, repetition. Department of Redundancy department. Oh, okay. I’ll, I’ll stop now. Raise, stop iteration. You’ll get that later. An iterable in Python is an object that can be iterated over.
00:34 You can go through its contents over and over. Objects, which are sequences in Python like lists, tuples, and strings are iterable, as are containers such as dictionaries and sets.
00:49
Let’s go play with a few of these in the REPL. Consider a list containing some numbers. I can use a for
loop to iterate over the object. In this case, numbers is an iterable.
01:05
The for
loop visits each item in the iterable, putting it in the variable num
, and then does whatever is in the body of the loop, which in this case is print something out.
01:18 This results in the items in the iterable, each being printed on their own line of output. You can do the same thing with a tuple.
01:33 Same idea with loop, and a similar result. Now let’s try a dictionary.
01:48
This dictionary contains people’s names mapped to their ages. Bob is 42, Jen is 51, and Raj is 15. You can use the for
loop to iterate over a dictionary. When you do so, the object extracted for each iteration is a key in the dictionary, which in this case is our names.
02:11
The dictionary object has methods for getting different kinds of things out of the dictionary. You can iterate over the results of the keys
method as well.
02:26
results in the same thing as iterating over the dictionary itself. The values
method can be used to iterate over the values in the dictionary.
02:41
And there are our ages. The items
method is used to iterate over a series of tuples, each of which contains the key and value pair. So
02:56
what gets printed is a tuple for each line containing the name and age. The for
loop allows you to use a tuple as the destination value for the iteration.
03:07 This causes the tuples to be unpacked into variables.
03:14 In this case, I’m still iterating over the response to items, but this time I’ve separated the results into variables instead of accessing them as a single tuple.
03:26 It’s just another way of doing the same thing, but shortcuts you into the variable you might be using inside of your loop.
03:35
So just how does the for
statement work? for
iterates over an object using the iterator protocol. A protocol in Python is a loosely defined interface that accomplishes something.
03:48 This typically is based on one or more methods that the protocol uses on a class. As the name iterator protocol might imply it expects an iterator object.
04:00 Iterator objects are used to iterate over an iterable, and at risk of this being a circular definition, an iterator object is one which implements the iterator protocol.
04:12
When the for
loop uses the protocol, it calls two built-in functions, iter()
and next()
. The first one, iter()
, is used on an iterable to create an iterator object.
04:25
I’ll show you this for real in a minute, but an example is calling iter()
on a list. What comes back is a list iterator object. The iterator object is responsible for returning each item to the loop.
04:37 Let’s stop and think about this for a second. Why use a separate object for iteration? Well, you’ve got to track the current item to be returned somewhere, and Python’s answer is to store that information in an iterator object.
04:51
You want this to be distinct from the iterable because you could have multiple iterators attached to the same iterable at the same time. Once for
has instantiated an iterator by calling iter()
, the subsequent step is to call the built-in function next()
. Each time next()
gets called on an iterator, the iterator returns the next item in the iteration.
05:15
If I am iterating over a list with three items, the first three calls to next()
each return, the first, second, and third items respectively.
05:23
And how does the for
loop know when to stop? That would be by catching the stop iteration exception. When an iterator is complete calling next()
on it raises a stop iteration.
05:35 Hence my joke. That’s probably a little generous. Hence my reference earlier about raising a stop iteration exception on the Department of Redundancy Department.
05:44 Repetition, jokes of repetition. Yeah. I’ll stop now. Let’s head back into the REPL and see this in action.
05:56
Once again, I have a list and when I call iter()
on the list, what I get back is a list iterator object. Let me do that again this time storing it in a variable,
06:12
and now I can call next()
on my iterator object. The result is the first number in the iterable list. If I call next()
again, I get the next value two
06:29 and dejavu gives me three. Finally, because the iterator is finished
06:37
next()
causes the stop iteration exception to be raised.
06:43 Now that you’ve seen the concept, next up, I’ll dive deeper into the structure of iterator objects and show you how to write your own.
Become a Member to join the conversation.