1
00:00:00,000 --> 00:00:04,990
For loops in Python are very useful for iterating over container types, and yet

2
00:00:05,000 --> 00:00:08,990
they are significantly different from for loops in other languages.

3
00:00:09,000 --> 00:00:13,990
In particular, for loops in other languages tend to have some sort of an index

4
00:00:14,000 --> 00:00:17,990
variable associated with them, and this can some times be useful. And we don't

5
00:00:18,000 --> 00:00:20,990
have that in the standard for loop in Python.

6
00:00:21,000 --> 00:00:25,990
So, let's take a look at how we can get that very easily using the

7
00:00:26,000 --> 00:00:26,990
enumerate function.

8
00:00:27,000 --> 00:00:33,990
Go ahead and make a working copy of for.py. I'll call it for-working.py and open

9
00:00:34,000 --> 00:00:41,990
that working file, and this is our little readlines example. And if we run this,

10
00:00:42,000 --> 00:00:46,990
You'll notice that we get the lines of text from the lines.txt file, which looks like this.

11
00:00:47,000 --> 00:00:53,990
They have that extra new line in them. We'll go ahead and get rid of that with end=" ".

12
00:00:54,000 --> 00:00:56,990
I am going to add something to this here. I am going to add the enumerate

13
00:00:57,000 --> 00:01:02,990
function, enumerate, and it goes like that.

14
00:01:03,000 --> 00:01:07,990
Enumerate actually returns two values. The first value is an index, and the

15
00:01:08,000 --> 00:01:10,990
second the value is the value from the iterator.

16
00:01:11,000 --> 00:01:16,990
I am going to print the index before the line, and we'll save this and run it, and

17
00:01:17,000 --> 00:01:17,990
you'll see what we get.

18
00:01:18,000 --> 00:01:21,990
So, the index is this extra number over here, and you'll notice that it starts at

19
00:01:22,000 --> 00:01:25,990
0, and that's actually a useful place for it because that's how you index

20
00:01:26,000 --> 00:01:27,990
containers in Python.

21
00:01:28,000 --> 00:01:31,990
So, let's take a look at an entirely different example here, and we'll take a

22
00:01:32,000 --> 00:01:37,990
string, and we'll iterate over that string.

23
00:01:38,000 --> 00:01:47,990
We'll use the enumerate function, and we will print the index and the character,

24
00:01:48,000 --> 00:01:51,990
just so you can get an idea of that looks like, save it and run it.

25
00:01:52,000 --> 00:01:55,990
So, here we have - I am going to scroll back up - each position in the string, with

26
00:01:56,000 --> 00:01:58,990
its index number and its character.

27
00:01:59,000 --> 00:02:04,990
Now, let's say that I am just interested in the letter S. So, I want to know

28
00:02:05,000 --> 00:02:10,990
which of these characters are the letter S. I can say if c='s' like that:

29
00:02:11,000 --> 00:02:20,990
print ('index'), number is an S, and I'll do a format I, like this.

30
00:02:21,000 --> 00:02:26,990
That will place the value of the index into this placeholder here.

31
00:02:27,000 --> 00:02:32,990
So, I'll save that, and run it, and now we know index 3 is an s, index 6 is an s

32
00:02:33,000 --> 00:02:40,990
and index 10 is an s. So, counting from 0, 1, 2, 3 there is an S, 4, 5, 6 is

33
00:02:41,000 --> 00:02:44,990
an S, 7, 8, 9 and 10.

34
00:02:45,000 --> 00:02:50,990
So 3, 6 and 10 are the Ss. So, sometime it's necessary to be able to find at what

35
00:02:51,000 --> 00:02:54,990
index, in an array type, a particular value is.

36
00:02:55,000 --> 00:02:58,990
This is a very useful pattern for doing that, and there is actually a lot of uses

37
00:02:59,000 --> 00:03:02,990
for having this index, and enumerate is how you can get that.

38
00:03:03,000 --> 00:03:06,990
So, this is how you get indexes and an iterator with for loop.

39
00:03:07,000 --> 00:03:12,990
Use this pattern here for, index and value and enumerate, which returns those two

40
00:03:13,000 --> 00:03:16,990
values, and the argument to enumerate is the iterator.

41
00:03:17,000 --> 00:03:27,000
Then you have the index and the value available inside your for loop.

