1
00:00:04,650 --> 00:00:08,130
Alright, so let's go through two
solutions to the no spam challenge I

2
00:00:08,130 --> 00:00:11,760
talked about, in the previous video. So
how did you get on with it? Did you have

3
00:00:11,760 --> 00:00:16,139
fun solving the challenge? Now your
solution may be different to mine. What's

4
00:00:16,139 --> 00:00:19,830
important is that you got the correct
items printed out. Alright, so I'm gonna

5
00:00:19,830 --> 00:00:24,120
start with a solution that removes the
spam from each of the inner lists.

6
00:00:24,120 --> 00:00:30,380
And we saw code how to do that, in earlier videos.
So starting down here, on line 12.

7
00:00:30,380 --> 00:00:33,480
We're going to create a for loop that
iterates over the menu, and that'll give

8
00:00:33,480 --> 00:00:41,940
us each of the inner lists in turn. So I'm
gonna type for meal in menu: Now inside

9
00:00:41,940 --> 00:00:46,680
this loop, we're going to use another for
loop to process each meal. So I'm going

10
00:00:46,680 --> 00:00:54,720
to type for space index in range left
parenthesis len, left parenthesis meal,

11
00:00:54,720 --> 00:01:03,300
right parentheses minus 1 comma minus 1 comma
minus 1, right parenthesis and a colon.

12
00:01:03,300 --> 00:01:07,259
Now we want to remove items from the list, so
we have to work backwards through the

13
00:01:07,259 --> 00:01:11,370
list, and that's why we've done the for
loop like that. Next we test to see if we

14
00:01:11,370 --> 00:01:18,320
found spam, and delete it, if we have. So
on this, line 14, I'm going to type if meal,

15
00:01:18,320 --> 00:01:23,220
left square bracket index right
square bracket, is equal to, two equal

16
00:01:23,220 --> 00:01:32,640
signs, spam in double quotes colon. Then
on line 15, del meal with index in left

17
00:01:32,640 --> 00:01:35,970
and right square brackets. So now that
we've done that, when the inner loop

18
00:01:35,970 --> 00:01:39,780
terminates we should have a list with no
spam. So let's print that out, in the

19
00:01:39,780 --> 00:01:44,000
outer loop. We'll do that on line 17, so
we'll come back here,

20
00:01:44,000 --> 00:01:51,880
and it needs to be the same level as the
for loop on line 13, print parentheses meal.

21
00:01:51,880 --> 00:01:55,320
Now if we put the print inside the
inner loop, your list will be printed

22
00:01:55,320 --> 00:02:00,660
each time around. We only want to print
it after we've finished removing spam from the list.

23
00:02:00,660 --> 00:02:03,440
Alright, so let's run the program
and check that there are eight meals and

24
00:02:03,440 --> 00:02:10,919
that none of them contain spam, and you
can see the output confirms that's the case.

25
00:02:10,919 --> 00:02:14,620
Now the output uses the default
string representation of a list.

26
00:02:14,620 --> 00:02:18,020
That means that each meal is enclosed in
square brackets. Now that

27
00:02:18,030 --> 00:02:21,000
may not be what you want. I'm going to
show you a way to deal with that, in the

28
00:02:21,000 --> 00:02:24,160
next video. For now though, that's one solution.

29
00:02:24,160 --> 00:02:29,740
What I'm going to do is comment out that code, then
create another loop to use the second approach.

30
00:02:34,680 --> 00:02:40,540
So I'll start with for meal in menu,
like we did before, colon

31
00:02:40,540 --> 00:02:44,459
Now, we're not going to mutate the list, this
time. That means we can work forwards

32
00:02:44,460 --> 00:02:50,160
through the meals. So I'm going to type
for item in meal:

33
00:02:50,160 --> 00:02:54,700
The next line, if item is not equal to
exclamation mark and equal sign,

34
00:02:54,700 --> 00:03:01,800
spam in double quotes colon. Then we're going
to print parentheses item. Then line 23,

35
00:03:01,800 --> 00:03:07,860
I'm going to go back to the same level
as the for loop on line 20, and type

36
00:03:07,860 --> 00:03:13,980
print left and right parentheses only. Alright,
so there's the code for the second

37
00:03:13,980 --> 00:03:20,060
approach. So we run it now. So you can see
here, we've got similar output.

38
00:03:20,060 --> 00:03:25,400
Eight meals, all without spam. So if your
solution produced the correct results,

39
00:03:25,410 --> 00:03:30,000
then well done. The items for each meal
are on a separate line here, though,

40
00:03:30,000 --> 00:03:35,060
which isn't ideal. We'll see how to fix that,
in the next few videos.

