WEBVTT 1 00:00:02.240 --> 00:00:05.570 So, in the previous two videos we've talked about lists and 2 00:00:05.570 --> 00:00:07.480 figured out how they work in Python. 3 00:00:07.480 --> 00:00:11.460 So, what I'm gonna do now is just run you through another example 4 00:00:11.460 --> 00:00:14.830 of a more complicated version and then move onto a challenge. 5 00:00:14.830 --> 00:00:17.850 Now, what we're gonna do is we're gonna use a concept we've used earlier, 6 00:00:17.850 --> 00:00:19.050 which is a meal in a cafe. 7 00:00:19.050 --> 00:00:21.570 So, what I'm going to do is type in some code. 8 00:00:21.570 --> 00:00:22.490 I'm going to actually delete what 9 00:00:22.490 --> 00:00:24.480 we've got on the screen here because we don't need it anymore. 10 00:00:25.990 --> 00:00:30.046 And I'm going to start typing menu equals two empty square brackets, 11 00:00:30.046 --> 00:00:31.280 which is an empty list. 12 00:00:31.280 --> 00:00:32.010 Then we do menu.append. 13 00:00:32.010 --> 00:00:35.880 Notice the square brackets. 14 00:00:35.880 --> 00:00:40.040 Egg, spam, bacon. 15 00:00:40.040 --> 00:00:41.410 Bacon like so. 16 00:00:41.410 --> 00:00:44.901 So that's our first entry and I'm just gonna copy the first part of this so 17 00:00:44.901 --> 00:00:48.120 I can copy it and paste it rather than type it each time. 18 00:00:48.120 --> 00:00:54.879 The next menu option is going to be egg, sausage, bacon. 19 00:00:54.879 --> 00:01:01.972 Next one is going to be egg, and spam. 20 00:01:01.972 --> 00:01:08.811 Next one, egg, bacon, spam. 21 00:01:08.811 --> 00:01:14.684 Next one, egg, bacon, 22 00:01:14.684 --> 00:01:18.890 sausage, spam. 23 00:01:18.890 --> 00:01:26.370 Next one is going to be spam, bacon, sausage, and more spam. 24 00:01:27.400 --> 00:01:27.900 Oops. 25 00:01:31.696 --> 00:01:33.639 Then we're going to have two more here. 26 00:01:33.639 --> 00:01:38.039 Spam, egg, spam. 27 00:01:38.039 --> 00:01:45.159 Spam, bacon, spam. 28 00:01:45.159 --> 00:01:50.451 And the last one we're gonna have is spam, 29 00:01:50.451 --> 00:01:53.399 egg, sausage, spam. 30 00:01:55.658 --> 00:01:58.020 So first things first, you might be wondering why is there so 31 00:01:58.020 --> 00:01:59.350 much spam in these meals? 32 00:01:59.350 --> 00:02:03.030 Well, the menu's actually taken from a Monty Python sketch. 33 00:02:03.030 --> 00:02:06.370 And if you want to search the Internet for Monty Python spam sketch, 34 00:02:06.370 --> 00:02:07.930 then you can find out more about that. 35 00:02:07.930 --> 00:02:10.750 You actually watch it on YouTube as well when you feel you need to take a break. 36 00:02:10.750 --> 00:02:14.630 So incidentally that sketch is the reason we now refer to junk email as a spam. 37 00:02:14.630 --> 00:02:17.480 But anyway so what we've done here is we've actually created the empty list 38 00:02:17.480 --> 00:02:19.060 as you saw on line one. 39 00:02:19.060 --> 00:02:21.717 And we then went through and appended each of the meals to it. 40 00:02:21.717 --> 00:02:25.548 And of course each of the meals as you'll now see in square brackets is actually 41 00:02:25.548 --> 00:02:28.406 listed its own right containing the details of that meal. 42 00:02:28.406 --> 00:02:29.940 So we can actually print something out. 43 00:02:29.940 --> 00:02:32.480 We can type print Menu. 44 00:02:34.460 --> 00:02:35.256 Actually run that. 45 00:02:37.735 --> 00:02:40.010 We can actually see the entries. 46 00:02:40.010 --> 00:02:43.413 So it's a little bit hard to see because it's actually menus within menus, 47 00:02:43.413 --> 00:02:46.494 but you can sort of see that there's the first on there on the screen, 48 00:02:46.494 --> 00:02:49.737 separated by a comma, and they've each got their own square brackets to 49 00:02:49.737 --> 00:02:52.431 indicate they're actually each a list in their own right. 50 00:02:52.431 --> 00:02:55.780 Now that we've actually got a list and we've got a lot of entries. 51 00:02:55.780 --> 00:02:57.920 We can actually start writing a for loop and 52 00:02:57.920 --> 00:03:00.870 actually testing to see how the in operator works. 53 00:03:00.870 --> 00:03:04.020 So we could do something like for meal in menu. 54 00:03:04.020 --> 00:03:09.323 Then we could put if not spam 55 00:03:09.323 --> 00:03:13.716 in meal, print meal. 56 00:03:13.716 --> 00:03:16.490 So if we actually run this and 57 00:03:16.490 --> 00:03:21.030 I'll just comment out this first line here, just to remove the confusion. 58 00:03:21.030 --> 00:03:24.980 And what that should do, and what that actually does is only shows the entry 59 00:03:24.980 --> 00:03:27.950 which is on line three that doesn't contain spam. 60 00:03:27.950 --> 00:03:30.640 And again each of the items on the menu are themselves lists. 61 00:03:30.640 --> 00:03:33.940 So the for loop here initially, for meal in menu, is going through and 62 00:03:33.940 --> 00:03:36.060 grabbing a list with each iteration. 63 00:03:36.060 --> 00:03:37.490 So the first time it goes through the for 64 00:03:37.490 --> 00:03:43.220 loop, the meal variable is set to the list, which is the first list in the list. 65 00:03:43.220 --> 00:03:44.170 So to speak. 66 00:03:44.170 --> 00:03:46.240 And from there we can put if not spam in meal, and 67 00:03:46.240 --> 00:03:49.660 that's actually searching through that individual list, the list that of 68 00:03:49.660 --> 00:03:53.060 course is sort of a sub-list of a major list, if that makes sense. 69 00:03:53.060 --> 00:03:56.310 Then we're actually determining that that wasn't in there, and we're only printing 70 00:03:56.310 --> 00:04:00.960 out the contents if the word spam wasn't found as one of the list entries. 71 00:04:00.960 --> 00:04:04.020 So again, you can see that Python is extremely powerful, 72 00:04:04.020 --> 00:04:07.870 the way it's been designed and how it actually handles this, is like this. 73 00:04:07.870 --> 00:04:08.810 Okay, so it's time for a challenge. 74 00:04:08.810 --> 00:04:13.700 What I am going to do is go to the first line and paste in the challenge. 75 00:04:15.070 --> 00:04:16.470 And that should be without spam. 76 00:04:18.080 --> 00:04:21.340 So let's actually talk now about what the challenge is actually about. 77 00:04:21.340 --> 00:04:24.590 So what you need to do is add to the program below. 78 00:04:24.590 --> 00:04:27.070 So that if it finds a meal without spam in it, 79 00:04:27.070 --> 00:04:29.950 that it then prints out each of the ingredients of the meal. 80 00:04:29.950 --> 00:04:33.910 At this stage you can see that we printed out everything, but you need to actually 81 00:04:33.910 --> 00:04:37.200 go through each individual item, rather than just printing the entire list. 82 00:04:37.200 --> 00:04:42.530 So you'll need to set up the menu as we did in lines five to 13 there. 83 00:04:42.530 --> 00:04:45.870 So type that in to start with, and then you'll need to modify the for 84 00:04:45.870 --> 00:04:47.060 loop in some way. 85 00:04:47.060 --> 00:04:48.680 This code down here. 86 00:04:48.680 --> 00:04:52.060 So then, basically prints out each ingredient separately, 87 00:04:52.060 --> 00:04:54.800 rather than just printing out just the list as it's currently doing. 88 00:04:54.800 --> 00:04:56.550 Okay? So, pause the video. 89 00:04:56.550 --> 00:04:59.340 Go away and actually see if you can come up with a solution to that. 90 00:04:59.340 --> 00:05:02.556 When you're finished, come back and check my solution. 91 00:05:07.615 --> 00:05:08.780 Okay so how did you get under? 92 00:05:08.780 --> 00:05:09.820 Did you figure it out? 93 00:05:09.820 --> 00:05:11.130 So we're actually on the right track here. 94 00:05:11.130 --> 00:05:13.576 We've actually got a lot of the code in build already. 95 00:05:13.576 --> 00:05:16.961 So we've got the first loop the outside loop with is going through each meal 96 00:05:16.961 --> 00:05:19.680 that's in the menu list then grabbing that meal. 97 00:05:19.680 --> 00:05:20.560 So that needs to stay there. 98 00:05:20.560 --> 00:05:23.570 We also need to put the if not in there, so 99 00:05:23.570 --> 00:05:26.890 that we only get the meals that haven't got spam in them. 100 00:05:26.890 --> 00:05:28.220 And we can print out this initial list. 101 00:05:28.220 --> 00:05:30.310 We can leave that in there like we're seeing on the screen. 102 00:05:30.310 --> 00:05:34.115 But what we then need to do is iterate through the list of 103 00:05:34.115 --> 00:05:37.415 entries in this particular meal and print those out. 104 00:05:37.415 --> 00:05:41.645 So to do that, we stay at this indentation level cuz we're only processing a meal 105 00:05:41.645 --> 00:05:42.735 if it doesn't contain spam. 106 00:05:42.735 --> 00:05:47.937 And we type in for ingredient in meal, 107 00:05:47.937 --> 00:05:54.943 colon print ingredient, so that is literally it. 108 00:05:54.943 --> 00:05:58.960 So we are taking it to the next level we're actually now going through the list 109 00:05:58.960 --> 00:06:00.480 from the meal. 110 00:06:00.480 --> 00:06:03.730 So first, the outer entry, the initial for loop, that was the master list, 111 00:06:03.730 --> 00:06:07.630 the list of all the menus, whereas the inner list for ingredient in meals is just 112 00:06:07.630 --> 00:06:11.250 as you can see it's actually the ingredients within a specific list. 113 00:06:11.250 --> 00:06:12.390 So, if we actually run that now. 114 00:06:13.750 --> 00:06:16.010 We finally get the output egg, sausage, bacon, and 115 00:06:16.010 --> 00:06:18.990 we're getting that because there's only one entry in here that hasn't got spam.. 116 00:06:18.990 --> 00:06:22.600 And then also, we're then cycling through that list that we retrieved, and 117 00:06:22.600 --> 00:06:24.410 actually printing out the individual items. 118 00:06:24.410 --> 00:06:24.930 So, that's it. 119 00:06:24.930 --> 00:06:26.670 That's the challenge, I hope you did well with that. 120 00:06:26.670 --> 00:06:28.370 And, I hope you've got a lot out of this section. 121 00:06:28.370 --> 00:06:29.900 And, I'll see you in the next video.