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