1
00:00:00,810 --> 00:00:05,490
-: Let me show you another useful function that we'll use

2
00:00:05,490 --> 00:00:06,630
just like range.

3
00:00:06,630 --> 00:00:08,550
Mind you, it's not as common as range.

4
00:00:08,550 --> 00:00:11,010
Range, you'll see it a lot in Python code

5
00:00:11,010 --> 00:00:13,920
but this other one is still very useful

6
00:00:13,920 --> 00:00:16,233
and it's called enumerate.

7
00:00:17,070 --> 00:00:18,630
Now what does it do?

8
00:00:18,630 --> 00:00:19,770
Let's have a look.

9
00:00:19,770 --> 00:00:23,610
I can do a for loop and I'll say for

10
00:00:23,610 --> 00:00:27,300
and we'll say character in enumerate.

11
00:00:27,300 --> 00:00:30,750
and then enumerate, we wrap around something

12
00:00:30,750 --> 00:00:32,820
that, well, we wanna enumerate.

13
00:00:32,820 --> 00:00:34,500
So let's see what that means.

14
00:00:34,500 --> 00:00:37,560
It returns an enumerate object.

15
00:00:37,560 --> 00:00:41,010
So iterable must be an other object that supports iteration

16
00:00:41,010 --> 00:00:43,740
so we have to give it something that enumerate

17
00:00:43,740 --> 00:00:45,270
can iterate over.

18
00:00:45,270 --> 00:00:46,200
So this time around

19
00:00:46,200 --> 00:00:50,490
let's just do a, a string that says hello.

20
00:00:50,490 --> 00:00:55,020
Now enumerate is special because what it's going to

21
00:00:55,020 --> 00:00:58,380
give us is, it's going to take an iterable object

22
00:00:58,380 --> 00:01:03,380
and gives you an index counter and the item at that index.

23
00:01:03,990 --> 00:01:05,519
What do I mean by that?

24
00:01:05,519 --> 00:01:09,930
Well, you can actually say i, character.

25
00:01:09,930 --> 00:01:14,460
So we're unpacking this and we're saying, hey

26
00:01:14,460 --> 00:01:17,910
print i and character.

27
00:01:17,910 --> 00:01:21,490
If I click run, look at that

28
00:01:23,220 --> 00:01:26,700
I get hello iterated over.

29
00:01:26,700 --> 00:01:28,830
Again, if we don't have enumerate

30
00:01:28,830 --> 00:01:31,890
then we would have just gotten hello.

31
00:01:31,890 --> 00:01:35,070
But we also, because we use enumerate, we get

32
00:01:35,070 --> 00:01:40,070
this i or index of each item and where it is in the index.

33
00:01:43,590 --> 00:01:45,390
So that if I had a list here

34
00:01:45,390 --> 00:01:47,553
let's say 1, 2, 3.

35
00:01:49,290 --> 00:01:53,610
Again, I'm able to use and access the index number

36
00:01:53,610 --> 00:01:55,143
of the item as well.

37
00:01:57,150 --> 00:01:59,433
What if we had a couple?

38
00:02:04,710 --> 00:02:07,773
Again, we get the index number.

39
00:02:09,630 --> 00:02:14,630
So enumerate is very useful if you need the index counter

40
00:02:15,180 --> 00:02:17,703
of the item that you're looping through.

41
00:02:19,050 --> 00:02:21,573
So let's do a little exercise.

42
00:02:22,470 --> 00:02:25,770
Let's say that we wanna create a script

43
00:02:25,770 --> 00:02:28,350
that enumerates a list

44
00:02:28,350 --> 00:02:30,360
of numbers 1 through 10.

45
00:02:30,360 --> 00:02:31,980
So we'll use range to create that.

46
00:02:31,980 --> 00:02:35,730
I'm going to say list, and then I'm going to say range.

47
00:02:35,730 --> 00:02:37,890
I know there's a lot of functions here.

48
00:02:37,890 --> 00:02:40,020
Range that is 100.

49
00:02:40,020 --> 00:02:42,030
Actually, let's do that.

50
00:02:42,030 --> 00:02:47,030
And I want to be told what the index

51
00:02:47,160 --> 00:02:50,400
of the number 50 is.

52
00:02:50,400 --> 00:02:53,880
So pause the video if you need to do that.

53
00:02:53,880 --> 00:02:56,970
Now here, I want it so that,

54
00:02:56,970 --> 00:02:58,830
let me make this a little bit bigger.

55
00:02:58,830 --> 00:03:02,280
I want it so that we only print the index

56
00:03:02,280 --> 00:03:05,070
of, let's say, the value 50.

57
00:03:05,070 --> 00:03:09,240
I wanna know where 50 is in the list.

58
00:03:09,240 --> 00:03:11,670
Now again, this is a simple example

59
00:03:11,670 --> 00:03:14,130
so you can probably already guess what the answer is

60
00:03:14,130 --> 00:03:15,750
but how would you code this so

61
00:03:15,750 --> 00:03:20,010
that only in here we get the index of 50.

62
00:03:20,010 --> 00:03:23,820
Or it says the index of 50 is whatever.

63
00:03:23,820 --> 00:03:25,890
Pause the video if you want to give it a go,

64
00:03:25,890 --> 00:03:27,603
otherwise, let's try this.

65
00:03:29,100 --> 00:03:33,450
I'm going to do a conditional that says

66
00:03:33,450 --> 00:03:37,353
if character is equal to 50,

67
00:03:38,310 --> 00:03:42,240
then I'm going to print index

68
00:03:42,240 --> 00:03:45,750
or I'm going to do an f string, and I'll say

69
00:03:45,750 --> 00:03:50,493
that the index of 50 is,

70
00:03:51,360 --> 00:03:55,120
and then I'll add the index in here

71
00:03:56,220 --> 00:03:57,423
so that if I run,

72
00:04:00,150 --> 00:04:03,483
I get index of 50 is 50.

73
00:04:04,530 --> 00:04:07,110
And just to confirm, if for example

74
00:04:07,110 --> 00:04:12,110
I just print here and let's say print i and character

75
00:04:12,210 --> 00:04:15,780
I click run, we see that, yep

76
00:04:15,780 --> 00:04:20,310
we have the indexes the same as the numbers.

77
00:04:20,310 --> 00:04:23,430
Again, not too difficult of a question, but you

78
00:04:23,430 --> 00:04:26,640
see here that we're able to extract this information.

79
00:04:26,640 --> 00:04:29,160
Again, not as useful as range

80
00:04:29,160 --> 00:04:31,170
but you will see it occasionally in code,

81
00:04:31,170 --> 00:04:34,590
so I wanted to make sure that you're not surprised by it.

82
00:04:34,590 --> 00:04:36,633
As you can see, it's not that difficult.

