1
00:00:04,740 --> 00:00:08,160
Let's now look at improving the
for loop that we introduced in the last

2
00:00:08,160 --> 00:00:13,109
video. So looking at that code on lines
29 and 30, we've got a for loop that

3
00:00:13,109 --> 00:00:19,199
assigns each of the values in our list,
to a variable called part. On line 30, the

4
00:00:19,199 --> 00:00:24,000
available_parts list has to
be searched, to get the index position of

5
00:00:24,000 --> 00:00:28,860
each part. If the list was sorted, we
could use a binary search, but our list

6
00:00:28,860 --> 00:00:34,160
isn't in order. Python has to check each
item, to see if it equals the value of part.

7
00:00:34,160 --> 00:00:37,700
The first part will be found
straight away - the second part will take

8
00:00:37,710 --> 00:00:41,910
a bit longer. Python will check if
monitor equals computer, then go on to

9
00:00:41,910 --> 00:00:46,590
check the second item in the list. That
matches, so that can return 1 as the

10
00:00:46,590 --> 00:00:51,390
index value. If there are hundreds or
thousands of items, finding the index

11
00:00:51,390 --> 00:00:55,440
positions will take a while. Now this is
a very common thing to have to do when

12
00:00:55,440 --> 00:00:59,160
programming, and fortunately, Python
provides a much more efficient way of

13
00:00:59,160 --> 00:01:04,799
doing it - and that's the enumerate
function. So enumerate returns each item

14
00:01:04,799 --> 00:01:09,900
with its index position. Let's see that
working . So I'm gonna make some changes to

15
00:01:09,900 --> 00:01:14,010
this code. So we've got for part in
available_parts - we're

16
00:01:14,010 --> 00:01:26,380
going to change that line: for number
comma then part in enumerate,

17
00:01:26,380 --> 00:01:32,880
with available parts in parentheses. So we've
got two variables in our for loop now,

18
00:01:32,880 --> 00:01:37,950
and then on the next line, we're gonna put
format, we're going to replace everything

19
00:01:37,950 --> 00:01:44,040
up until this comma, with number. So we've
now got number. We still need to add the

20
00:01:44,040 --> 00:01:47,880
plus 1 - put that back
because, of course, indexing starts at 0

21
00:01:47,880 --> 00:01:53,260
comma part. So we'll replace the index
method call entirely, with number.

22
00:01:53,260 --> 00:01:57,119
So let's actually run the program to make
sure that it works, and you can see we've

23
00:01:57,119 --> 00:02:02,340
still got the same output. So we've got
our menu with the correct numbers. And it

24
00:02:02,340 --> 00:02:09,419
will still work - we can add items. Option
7 won't work, and that's because we need

25
00:02:09,419 --> 00:02:13,919
to tidy up the first part of the code - the
code up there in our in, and also in our

26
00:02:13,919 --> 00:02:18,029
elif blocks. So I'm going to exit the
program now. So overall, the menu is

27
00:02:18,029 --> 00:02:23,140
working,
which is good. Alright, so when I

28
00:02:23,140 --> 00:02:26,560
discussed for loops in Section 4, I
said that a for loop starts with the

29
00:02:26,560 --> 00:02:31,870
word for, and then the names of one or
more variables. If the object that's

30
00:02:31,870 --> 00:02:36,250
iterated over contains more than one
value, you can use more than one variable

31
00:02:36,250 --> 00:02:41,830
in the for loop and that's what we're
doing on line 29. enumerate returns pairs

32
00:02:41,830 --> 00:02:47,830
of values - we get the index position and
the item, as a pair of values. The first

33
00:02:47,830 --> 00:02:52,780
value is the index position and the
second value is the item. So looking

34
00:02:52,780 --> 00:02:57,160
again, at our for loop on line 29, each
time around the loop, number will be the

35
00:02:57,160 --> 00:03:02,410
index position and part will be the item
from the list. Good news is, we can use

36
00:03:02,410 --> 00:03:07,240
enumerate with any iterable type. All
sequences are iterable, so let's see

37
00:03:07,240 --> 00:03:12,820
another example here. I'm going to create
a new Python file called enumerate.example.

38
00:03:21,820 --> 00:03:29,760
I'm going to type for index comma
character in enumerate, and in

39
00:03:29,769 --> 00:03:36,750
parentheses double quotes, and within the
double quotes I need to type abcdefgh.

40
00:03:38,190 --> 00:03:47,200
Then on line two I'm going to type print
index character. That should have been a

41
00:03:47,200 --> 00:03:52,390
colon on the end of the line, of course. Now note that
there's nothing special about the

42
00:03:52,390 --> 00:03:56,350
variable names I'm using here. You can
see that I've used indexing character here.

43
00:03:56,350 --> 00:04:00,070
Index will get the index position and
character gets each character from the

44
00:04:00,070 --> 00:04:06,489
string, in turn. Let's run the program to
make sure that it works, and you can see

45
00:04:06,489 --> 00:04:12,040
the index numbers match the characters
in the string. So make a note about the

46
00:04:12,040 --> 00:04:15,940
enumerate function. It's built into
Python and provides a very efficient way

47
00:04:15,940 --> 00:04:21,130
to get indexes, as well as the values in
a loop. Alright, so in the next video,

48
00:04:21,130 --> 00:04:25,090
we'll go back to our computer parts menu,
and tidy up the conditions that append

49
00:04:25,090 --> 00:04:29,580
the parts to the list. See you in the
next video.

