1
00:00:00,800 --> 00:00:06,470
Let me show you another useful function that we'll use, just like a range.

2
00:00:06,620 --> 00:00:08,920
Mind you, it's not as common as range range.

3
00:00:08,930 --> 00:00:16,170
You'll see it a lot in Python code, but this other one is still very useful and it's called enumerate.

4
00:00:17,060 --> 00:00:17,960
Now, what does it do?

5
00:00:18,620 --> 00:00:19,260
Let's have a look.

6
00:00:19,760 --> 00:00:28,700
I can do a for loop and I'll say four and we'll say character in enumerate and then enumerate.

7
00:00:28,700 --> 00:00:32,840
We wrap around something that, well, we want to enumerate.

8
00:00:32,840 --> 00:00:34,280
So let's see what that means.

9
00:00:34,490 --> 00:00:36,820
It returns and enumerate to object.

10
00:00:37,550 --> 00:00:41,050
So iterable must be an other object that supports iteration.

11
00:00:41,060 --> 00:00:45,020
So we have to give it something that enumerate can iterate over.

12
00:00:45,260 --> 00:00:49,650
So this time around let's just do a a string that says hello.

13
00:00:50,480 --> 00:00:58,400
Now enumerate is special because what it's going to give us is it's going to take an iterable object

14
00:00:58,400 --> 00:01:00,530
and gives you an index counter.

15
00:01:00,950 --> 00:01:03,200
And the item that index.

16
00:01:03,980 --> 00:01:09,790
What I mean by that, well, you can actually say I CAAMA character.

17
00:01:09,920 --> 00:01:19,070
So we're unpacking this and we're saying, hey, print I and character, if I click run.

18
00:01:23,190 --> 00:01:31,220
I get hello iterated over again, if we don't have enumerate, then we would have just gotten hello.

19
00:01:31,860 --> 00:01:42,120
But we also because we use enumerate, we get this eye or index of each item and where it is in the

20
00:01:42,120 --> 00:01:47,400
index so that if I had a list here, let's say one, two, three.

21
00:01:49,240 --> 00:01:55,020
Again, I'm able to use and access the index number of the item as well.

22
00:01:57,160 --> 00:01:59,290
What if we had a couple?

23
00:02:04,680 --> 00:02:07,620
Again, we get the index number.

24
00:02:09,620 --> 00:02:17,570
So innumerate is very useful if you need the index counter of the item that you're looping through.

25
00:02:19,050 --> 00:02:21,410
So let's do a little exercise.

26
00:02:22,460 --> 00:02:30,860
Let's say that we want to create a script that enumerates a list of numbers one through ten, so we'll

27
00:02:30,860 --> 00:02:35,710
use range to create that I'm going to say list and then I'm going to say range.

28
00:02:35,720 --> 00:02:37,370
I know there's a lot of functions here.

29
00:02:37,880 --> 00:02:41,240
Range that is one hundred actually list to that.

30
00:02:41,990 --> 00:02:49,760
And I want to be told what the index of the number 50 is.

31
00:02:50,390 --> 00:02:52,960
So pause the video if you need to do that.

32
00:02:53,870 --> 00:03:00,980
Now, here, I want it so that when you make this a little bit bigger, I want it so that we only print

33
00:03:01,250 --> 00:03:05,090
the index of, let's say, the value fifty.

34
00:03:05,090 --> 00:03:08,360
I want to know where 50 is in the list.

35
00:03:09,230 --> 00:03:14,510
Now, again, this is a simple example, so you can probably already guess what the answer is, but

36
00:03:14,510 --> 00:03:22,340
how would you code this so that only in here we get the index of fifty or it says the index of fifty

37
00:03:22,340 --> 00:03:23,270
is whatever.

38
00:03:23,810 --> 00:03:25,610
Pause the video if you want to give it a go.

39
00:03:25,880 --> 00:03:27,410
Otherwise, let's try this.

40
00:03:29,080 --> 00:03:37,210
I'm going to do a conditional and says if character is equal to 50.

41
00:03:38,340 --> 00:03:39,990
Then I'm going to print.

42
00:03:41,440 --> 00:03:53,230
Index or I'm going to do an F string, and I will say that the index of 50 is and then I'll add the

43
00:03:53,920 --> 00:03:55,060
index in here.

44
00:03:56,200 --> 00:03:57,280
So that if I run.

45
00:04:00,110 --> 00:04:03,380
I get index of 50 is 50.

46
00:04:04,470 --> 00:04:12,660
And just to confirm if, for example, I just print here and let's say print I and character, I click

47
00:04:12,660 --> 00:04:13,020
run.

48
00:04:14,480 --> 00:04:23,210
We see that map, we have the index is the same as the numbers, again, not too difficult of a question,

49
00:04:23,210 --> 00:04:29,510
but you see here that we're able to extract this information again, not as useful as range, but you

50
00:04:29,510 --> 00:04:31,180
will see it occasionally in code.

51
00:04:31,190 --> 00:04:34,210
So I wanted to make sure that you're not surprised by it.

52
00:04:34,550 --> 00:04:36,530
As you can see, it's not that difficult.
