1 00:00:00,000 --> 00:00:04,719 [MUSIC] 2 00:00:04,719 --> 00:00:07,680 One of the most common things to do with lists is to loop or 3 00:00:07,680 --> 00:00:09,680 iterate through every item. 4 00:00:09,680 --> 00:00:12,330 Now, typically we iterate through each value and 5 00:00:12,330 --> 00:00:14,680 then run some code against that item. 6 00:00:14,680 --> 00:00:18,830 Now, the most common way to do this is with a for-in loop. 7 00:00:18,830 --> 00:00:22,340 We can use the for-in loop because the list is iterable. 8 00:00:22,340 --> 00:00:24,690 That is, it's able to be iterated. 9 00:00:24,690 --> 00:00:28,150 Let's take a look at this ability in the REPL. 10 00:00:28,150 --> 00:00:33,110 Okay, so let's take a look at this meeting.py file in interactive mode. 11 00:00:33,110 --> 00:00:39,670 So we'll kick this off, python -i meeting.py. 12 00:00:39,670 --> 00:00:41,960 And we have an attendees list. 13 00:00:41,960 --> 00:00:43,050 Remember? 14 00:00:43,050 --> 00:00:44,750 So we have our attendees here. 15 00:00:44,750 --> 00:00:46,960 And this is something we can iterate over. 16 00:00:46,960 --> 00:00:49,750 And the syntax is pretty clean. 17 00:00:49,750 --> 00:00:53,858 It's four and in our case the thing is attendee. 18 00:00:53,858 --> 00:00:58,328 And this will be the variable that our item is stored in for each iteration. 19 00:00:58,328 --> 00:01:04,012 And then the key word, in, and then our list, which is again, attendees. 20 00:01:04,012 --> 00:01:07,551 And then we need to do a colon to start the block. 21 00:01:07,551 --> 00:01:10,654 And then we can work with each one of these items cuz they're already 22 00:01:10,654 --> 00:01:12,240 stored in the attendee variable. 23 00:01:12,240 --> 00:01:13,478 So, let's just print it out. 24 00:01:13,478 --> 00:01:18,129 So I'll say print(attendee). 25 00:01:18,129 --> 00:01:20,049 Awesome. 26 00:01:20,049 --> 00:01:24,725 So, I just want you to imagine that each time through the loop that this variable, 27 00:01:24,725 --> 00:01:27,625 this attendee here is set to the next item from here. 28 00:01:27,625 --> 00:01:29,165 So it just goes one through each right? 29 00:01:29,165 --> 00:01:31,835 So it goes, Ken, Alena, Trevor, Ashley, James. 30 00:01:31,835 --> 00:01:34,905 And just sets that attendee each time through the loop. 31 00:01:34,905 --> 00:01:37,175 Now, here's something that you might not expect. 32 00:01:37,175 --> 00:01:40,385 The last value is actually still available with that label 33 00:01:40,385 --> 00:01:42,460 even after the loop is done. 34 00:01:42,460 --> 00:01:43,740 So if I just type attendee here, 35 00:01:43,740 --> 00:01:47,210 you'll see that's set to the last thing we looped through. 36 00:01:47,210 --> 00:01:48,880 So attendee is still set. 37 00:01:48,880 --> 00:01:51,476 I'm not sure you'll ever need that, but you never know. 38 00:01:51,476 --> 00:01:55,113 One more think while we're here in the REBEL, the end keyword. 39 00:01:55,113 --> 00:01:59,261 Just like how you can use it to see if a string is inside another string, or 40 00:01:59,261 --> 00:02:03,320 we can also use it to see if one item is in the list like for instance. 41 00:02:03,320 --> 00:02:04,950 I can see if actually Ashley was in our list. 42 00:02:04,950 --> 00:02:10,080 I will just say hey, is Ashley in attendees? 43 00:02:10,080 --> 00:02:12,100 True she is, awesome. 44 00:02:12,100 --> 00:02:15,850 So let's pop out of this and let's switch over. 45 00:02:15,850 --> 00:02:18,102 To our wish list file. 46 00:02:20,332 --> 00:02:24,948 So I was thinking how about we make this show every one of the items in our 47 00:02:24,948 --> 00:02:26,230 wish list. 48 00:02:26,230 --> 00:02:27,350 Let's just do the entire list. 49 00:02:27,350 --> 00:02:30,224 So, let's see, so we'll say, let's make a little header here. 50 00:02:30,224 --> 00:02:36,140 So we'll do ("Books:"), and we'll say for book, that's our thing, 51 00:02:36,140 --> 00:02:41,613 in books:, so that's gonna loop through each one of this will be set. 52 00:02:41,613 --> 00:02:46,634 That'll be the book, that'll be the book, that'll be that book, right? 53 00:02:46,634 --> 00:02:49,898 And so, I wanna make sure that this is on 4, space is 4 here. 54 00:02:51,897 --> 00:02:53,420 Don't forget that. 55 00:02:53,420 --> 00:02:54,288 Here we go. 56 00:02:54,288 --> 00:02:57,881 For booking book,so now we want to print them. 57 00:02:57,881 --> 00:03:01,708 And let's do a little bullet, we will do a little asterisks there. 58 00:03:01,708 --> 00:03:03,710 You could use some Unicode magic if you wanted to. 59 00:03:05,842 --> 00:03:09,520 So we'll look through each one of these and then print out the books, cool. 60 00:03:09,520 --> 00:03:12,326 Let's go ahead, let's save that and then run it. 61 00:03:12,326 --> 00:03:18,410 So python -i wishlist.py. 62 00:03:18,410 --> 00:03:22,820 Awesome, beautiful, you know what while I'm here 63 00:03:22,820 --> 00:03:25,860 id like to add another type of product to my wish list. 64 00:03:25,860 --> 00:03:30,523 So my other addiction is video games I've added my wish list from a couple days ago 65 00:03:30,523 --> 00:03:33,620 to the teachers note so lets just copy and paste that. 66 00:03:35,060 --> 00:03:35,940 Paste that up here. 67 00:03:38,460 --> 00:03:41,000 So there we go video games. 68 00:03:41,000 --> 00:03:42,890 Breath of a Wild, wish I was playing right now. 69 00:03:43,950 --> 00:03:46,290 And things don't stay on this list very long. 70 00:03:46,290 --> 00:03:48,170 Confession I've already bought all these video games. 71 00:03:48,170 --> 00:03:49,240 I'm a little bit obsessed. 72 00:03:49,240 --> 00:03:50,510 They're all so good. 73 00:03:50,510 --> 00:03:52,920 Anyways, let's just display my outdated list. 74 00:03:52,920 --> 00:03:57,690 So I think I'd like to do the same thing, I'd like to add a title and 75 00:03:57,690 --> 00:04:02,170 then do a layout, I suppose I could just copy and paste the code, but 76 00:04:02,170 --> 00:04:06,398 my don't repeat yourself my dry spidey sense is going off. 77 00:04:06,398 --> 00:04:08,520 I don't wanna duplicate that code, 78 00:04:08,520 --> 00:04:11,990 now what if I want to change the way it's displayed, 79 00:04:11,990 --> 00:04:16,400 I have to do that in two places so here let's solve that problem with a function. 80 00:04:16,400 --> 00:04:20,510 So how about I make a function that expects parameters of the display name? 81 00:04:20,510 --> 00:04:23,690 You know, like, is it a book or a video game, and also the list. 82 00:04:23,690 --> 00:04:24,750 Let's do that. 83 00:04:24,750 --> 00:04:27,850 But first, let's take a quick break, because I want to show you the gotchas 84 00:04:27,850 --> 00:04:31,480 surround the passing a mutable object, like a list around. 85 00:04:31,480 --> 00:04:33,640 Take a quick break, and come back refreshed and 86 00:04:33,640 --> 00:04:35,580 we'll tackle this mutability issue head on.