#100DaysOfCode in Python Transcripts
Chapter: Days 19-21: Iteration with itertools
Lecture: Iteration Refresher

Login or purchase this course to watch this video and the rest of the course contents.
0:00 All right, so let's discuss what iteration is in Python. When we say something is iterable, we're saying that you're able to iterate over it,
0:11 you're able to move through it one item by item, okay? So the easiest way to demonstrate this is just to get a simple list.
0:22 So we'll create a quick list here of numbers. Let's go with the numbers 1 through 10. Okay, oops, what am I doing? 1 through 10. And we know that,
0:42 and we know that number is 10 digits, right? Nice and easy. Now if we're iterating over that, we're going to run a full loop, so for i in numbers,
0:55 in number, I should say, print i. This is something we've all done before, we all know what this is, but what's happening behind the scenes?
1:06 Okay, yes we're running a full loop, but really what is that full loop doing to iterate over the number list and give you the numbers?
1:18 Well, it's actually calling the iter dunder method, or iter protocol, okay? So we can see this if we actually drill in to the number list.
1:31 We can see that it is actually calling iter or it's capable of being iterated over. Okay, so we've got iter and dur number
1:44 and we get true, so it's in there. This is an iterable item, we can iterate over it, okay? Now another way to demonstrate
1:53 an iterable item is to use next. We've all seen next before, or hopefully you've seen next. And what happens is when you run next on an iterator,
2:04 when it finishes iterating over the sequence, over the list or the string or whatever it happens to be,
2:11 when it finishes this, it then gives you an error, okay, it gives you a StopIteration error because it's only going to iterate through it
2:21 up until the end and then it will stop. So now that we know that iter is actually what's being called in the background, we can use that,
2:31 okay, we can use that with next. Now if you haven't heard of next, next is a little function you can run against an iterator.
2:42 And what it will do is it will pass over, it will iterate over that iterator, that list or that string or whatever it is,
2:51 and it will continue through it until it hits the end. When it hits the last item, when it hits the last character,
2:58 it will actually give you a StopIteration error, okay? So we'll demonstrate that with a string called, let's call it string, okay?
3:07 So it equals iter, we're actually calling iter now over the word string. Okay, so we know that when you iterate over this word here, over this string,
3:20 you're going to get the letters one by one, right? So if we call next on that, we'll get the letter S, okay? Now let's copy and paste this a few times,
3:32 just so we can demonstrate. We get the T, we get the R, I, N, G. But then, when we run it one more time, we get the stop iteration, okay?
3:46 And that is because we're calling next directly. That's because it's hit the end and it's taken care of.
3:54 It gets to the end but then it's not going to go any further because it knows it's already finished. Now, when you run a full loop,
4:02 so for character in string, print character, we get the letters, but we don't get this error, we don't get the StopIteration error,
4:16 and that's because it's actually built in to the full loop so that it's not going to give you that error. It's expected, okay, so it knows
4:26 that it's hit the end and it's not going to actually sit there and give you the error. And that's it, that's a basic coverage of iteration.
4:34 You see it's just going through each object, each item, one by one, to get to the end. Now, there is iter tools, a nice series of functions
4:45 that are just so cool and make iteration a lot more interesting and a lot easier, so that's what we're going to look at.

Talk Python's Mastodon Michael Kennedy's Mastodon