1 00:00:00,590 --> 00:00:03,520 All right, so we wanna come up with a handy function that 2 00:00:03,520 --> 00:00:07,900 will allow us to display each wish list in a standard sort of fashion. 3 00:00:07,900 --> 00:00:11,248 This is, as you know, a wonderful place to use a function. 4 00:00:11,248 --> 00:00:16,595 So we want to display our heading, just like we did here like we say this, books. 5 00:00:16,595 --> 00:00:20,685 And we also wanna display our list so we're gonna loop through list. 6 00:00:20,685 --> 00:00:22,325 So we got this, right? 7 00:00:22,325 --> 00:00:23,155 Let's make a new function. 8 00:00:23,155 --> 00:00:26,793 So the function will look like, let's see, 9 00:00:26,793 --> 00:00:32,160 we're gonna define the new function called display wish list. 10 00:00:32,160 --> 00:00:35,310 And first we want to know what that heading is. 11 00:00:35,310 --> 00:00:37,607 Let's call that display_name. 12 00:00:37,607 --> 00:00:40,280 So that'll be books or video games. 13 00:00:40,280 --> 00:00:42,400 And then the items that we want. 14 00:00:42,400 --> 00:00:46,975 So we'll just pass in wishes cuz it's a wish whether it's a book or 15 00:00:46,975 --> 00:00:47,970 it's a video game list. 16 00:00:47,970 --> 00:00:50,170 This is the list of there that we're gonna pass it. 17 00:00:50,170 --> 00:00:51,450 Awesome. 18 00:00:51,450 --> 00:00:53,635 So first we'll just print out that header. 19 00:00:53,635 --> 00:00:58,236 So we''ll say display_name and 20 00:00:58,236 --> 00:01:01,731 we'll make that a colon. 21 00:01:01,731 --> 00:01:07,660 There we go You know what, let's go ahead, let's do this suggested gift thing. 22 00:01:07,660 --> 00:01:10,872 Let's go ahead and put this as part of our function. 23 00:01:10,872 --> 00:01:14,910 Cause we'll suggest a gift from each one of them. 24 00:01:14,910 --> 00:01:16,620 So, hm, you know what I was just thinking? 25 00:01:16,620 --> 00:01:19,380 If we show a suggested gift from the list, 26 00:01:19,380 --> 00:01:22,200 we shouldn't show it in the other part, right? 27 00:01:22,200 --> 00:01:26,800 So I guess what we want to do is we just wanna pop that first one off, right? 28 00:01:26,800 --> 00:01:28,340 I mean, what could go wrong? 29 00:01:28,340 --> 00:01:34,679 So we'll say suggested_gift equals wishes.pop. 30 00:01:34,679 --> 00:01:36,770 And we'll get the first one right? 31 00:01:36,770 --> 00:01:38,040 So we want pop zero. 32 00:01:39,530 --> 00:01:41,590 Okay, so let's make this stand out a little bit. 33 00:01:41,590 --> 00:01:45,405 We'll say print, we'll add some arrows here at the front. 34 00:01:45,405 --> 00:01:48,742 Let's really stand out and we'll print out that suggested_gift. 35 00:01:51,488 --> 00:01:54,600 And we're removed that from the list. 36 00:01:54,600 --> 00:01:56,970 So we can probably just do what we did before. 37 00:01:56,970 --> 00:01:59,240 So we just wanna loop through each of the wishes. 38 00:01:59,240 --> 00:02:05,375 So we can say, for wish in wishes and we will print. 39 00:02:05,375 --> 00:02:10,195 Let's see so we used the star before for bullet, plus wish, there we go, 40 00:02:10,195 --> 00:02:14,105 and then at the very end let's just add a blank print here 41 00:02:15,920 --> 00:02:19,960 after the display wish list, so that will keep things separate. 42 00:02:19,960 --> 00:02:23,410 Cool, so now we should be able to just call that function. 43 00:02:23,410 --> 00:02:27,740 So we'll say display_wishlist and 44 00:02:27,740 --> 00:02:33,340 we will call that for the display name is books. 45 00:02:33,340 --> 00:02:37,580 And there that wish, the list of wishes we have that's called books. 46 00:02:37,580 --> 00:02:39,120 And let's do it for video games. 47 00:02:39,120 --> 00:02:41,086 So I can just copy this. 48 00:02:41,086 --> 00:02:45,356 And instead of books, 49 00:02:45,356 --> 00:02:49,410 we have video games. 50 00:02:51,010 --> 00:02:52,490 Awesom, that feel pretty clean, right? 51 00:02:52,490 --> 00:02:56,036 Let's git rid of these other things Here. 52 00:03:00,037 --> 00:03:00,730 There you go. 53 00:03:00,730 --> 00:03:03,140 Yeah nice and clean. 54 00:03:03,140 --> 00:03:04,230 So we've got the two lists, 55 00:03:04,230 --> 00:03:07,780 we have a function, we're gonna call it let's see what it looks like. 56 00:03:07,780 --> 00:03:16,190 So we'll say python Awesome, looks like we go it. 57 00:03:16,190 --> 00:03:18,500 I'm gonna scroll this up a little bit and run that again. 58 00:03:18,500 --> 00:03:21,310 Sometimes the scrolling gets a little wacky here. 59 00:03:21,310 --> 00:03:24,250 Cool, so we've pulled one off and we pulled the second one off. 60 00:03:24,250 --> 00:03:26,520 And it looks right, looks good, right? 61 00:03:29,180 --> 00:03:31,380 There's a little bug in our program though. 62 00:03:31,380 --> 00:03:32,020 Let's take a look. 63 00:03:32,020 --> 00:03:34,640 Do you see what that bug is? 64 00:03:34,640 --> 00:03:36,074 Here, this'll really demonstrate it. 65 00:03:36,074 --> 00:03:38,920 Let's do this, I'm gonna go ahead, I'm gonna call this again one more time. 66 00:03:45,300 --> 00:03:46,180 That's weird. 67 00:03:46,180 --> 00:03:50,056 The second time through, it's a little bit shorter and it's a different suggestion. 68 00:03:50,056 --> 00:03:55,930 That's because we're changing the list with that pop command. 69 00:03:55,930 --> 00:04:00,180 This pop command here is actually changing this list. 70 00:04:00,180 --> 00:04:03,000 Now, I made that happen on purpose to really highlight 71 00:04:03,000 --> 00:04:06,690 the caution that you should take when dealing with mutable objects. 72 00:04:06,690 --> 00:04:11,640 So let's think about how a user of this function might feel. 73 00:04:11,640 --> 00:04:15,490 They pass their list in here to get a function called display wishlist and 74 00:04:15,490 --> 00:04:18,600 I'm pretty sure that they aren't gonna expect their list to get an an item 75 00:04:18,600 --> 00:04:19,730 removed from it. 76 00:04:19,730 --> 00:04:21,340 That's gonna come as a shock. 77 00:04:21,340 --> 00:04:25,200 So remember, these parameters can be thought of like applying the label, 78 00:04:25,200 --> 00:04:29,120 display name equals books and wishes equals books. 79 00:04:29,120 --> 00:04:32,330 So we've applied a label to that object that was passed in. 80 00:04:32,330 --> 00:04:35,915 Now, the label only lasts while the function is running and then it's removed. 81 00:04:35,915 --> 00:04:38,645 But during that function execution, the label, 82 00:04:38,645 --> 00:04:41,185 wishes, it's referring to the same object. 83 00:04:41,185 --> 00:04:43,315 Now in this case, it was the video games list. 84 00:04:43,315 --> 00:04:48,512 That's why when we call pop, it changes the object because it's the same object. 85 00:04:48,512 --> 00:04:50,842 So there are better ways to do this. 86 00:04:50,842 --> 00:04:54,312 One rule to follow is to not modify someone else's objects 87 00:04:54,312 --> 00:04:57,382 unless you are explicit about it in your method name. 88 00:04:57,382 --> 00:05:01,350 If you find yourself needing to change a list, like we did here just for 89 00:05:01,350 --> 00:05:04,910 display purposes, you can use the copy method. 90 00:05:04,910 --> 00:05:09,740 The copy method on lists allows you to make a copy of the list and 91 00:05:09,740 --> 00:05:11,840 this leaves the previous one intact. 92 00:05:11,840 --> 00:05:13,020 So let's go ahead and do that. 93 00:05:13,020 --> 00:05:18,120 So I'm going to make a new copy of the list when we come in here and 94 00:05:18,120 --> 00:05:19,118 I'm gonna call it items. 95 00:05:19,118 --> 00:05:23,540 So items equals wishes.copy. 96 00:05:23,540 --> 00:05:30,110 Not.copy and then instead of wishes here, I'm gonna say items. 97 00:05:30,110 --> 00:05:34,621 And then we can say for item and 98 00:05:34,621 --> 00:05:39,131 items, item. 99 00:05:40,800 --> 00:05:44,790 So items is now a completely unrelated copy of wishes. 100 00:05:44,790 --> 00:05:47,550 Now, I can change items all I want and it won't change this, just a copy. 101 00:05:47,550 --> 00:05:51,010 So here watch, we'll run it again and you'll see both of them. 102 00:05:51,010 --> 00:05:52,700 It stayed the same each time. 103 00:05:52,700 --> 00:05:55,840 It didn't change that master video games list. 104 00:05:55,840 --> 00:05:58,730 Now I should point out that you could actually solve this problem that we're 105 00:05:58,730 --> 00:06:01,480 having here by not removing the element from the list at all. 106 00:06:01,480 --> 00:06:04,810 And only iterating through the lists starting at the second element. 107 00:06:04,810 --> 00:06:07,120 You could use what is known as a slice. 108 00:06:07,120 --> 00:06:10,800 We'll get to that here in a bit, and check the teacher's notes for a sneak peek. 109 00:06:10,800 --> 00:06:15,600 Another thing that I want you to be careful of is loops, and mutability. 110 00:06:15,600 --> 00:06:17,580 So let's pop open a repel really quick. 111 00:06:17,580 --> 00:06:19,483 Let's pop open, we'll say python 112 00:06:24,123 --> 00:06:27,090 Let's take an example from one of those video games I was playing. 113 00:06:27,090 --> 00:06:28,030 Let's use Zelda. 114 00:06:28,030 --> 00:06:30,170 It's an adventure game, and you collect items, and 115 00:06:30,170 --> 00:06:32,050 you keep them in your inventory. 116 00:06:32,050 --> 00:06:34,980 So a typical inventory might look something like this. 117 00:06:34,980 --> 00:06:40,340 So we have inventory, do a list and you might have a shield, 118 00:06:40,340 --> 00:06:43,650 definitely have apples that's how you go ahead and eat things. 119 00:06:43,650 --> 00:06:47,350 And then you have a sword, maybe you'll have a bow like for a bow and arrow, 120 00:06:47,350 --> 00:06:48,230 a boomerang. 121 00:06:49,960 --> 00:06:54,290 All right, basic inventory from an adventure game, right? 122 00:06:54,290 --> 00:06:56,890 So one thing that we haven't looked at yet 123 00:06:56,890 --> 00:07:00,650 is that you can remove an element from a list by value. 124 00:07:00,650 --> 00:07:02,830 So you could say inventory. 125 00:07:04,090 --> 00:07:06,590 And there is a method called remove and 126 00:07:06,590 --> 00:07:08,790 let's say that we wanted to get rid of that apple. 127 00:07:08,790 --> 00:07:11,840 So we're gonna remove the apple from our inventory and 128 00:07:11,840 --> 00:07:15,570 now if you look at inventory, you'll see that it's gone. 129 00:07:15,570 --> 00:07:19,910 A common problem that happens is that you mutate the list 130 00:07:19,910 --> 00:07:21,680 while you're looping over it. 131 00:07:21,680 --> 00:07:22,320 Like for instance, 132 00:07:22,320 --> 00:07:25,550 let's say that you wanted to drop everything from your inventory. 133 00:07:25,550 --> 00:07:28,870 You might think that you could do something like this, for item 134 00:07:28,870 --> 00:07:32,950 in inventory, so we're just gonna loop though every single one of these items. 135 00:07:32,950 --> 00:07:39,880 And, we'll say, inventory.remove, and we are going to remove that item. 136 00:07:39,880 --> 00:07:42,220 That seems like it make sense, right? 137 00:07:42,220 --> 00:07:46,060 But we were looping over it, so what happens is you get this weird 138 00:07:46,060 --> 00:07:50,230 unexpected result, you still got a sword and a boomerang left. 139 00:07:50,230 --> 00:07:52,740 So again, this is where copy saves the day. 140 00:07:52,740 --> 00:07:55,160 You just loop over a copy of the array, like so. 141 00:07:55,160 --> 00:07:57,120 Let's do this. So let's come up here. 142 00:07:57,120 --> 00:07:59,140 Let's reset our inventory. 143 00:07:59,140 --> 00:08:04,968 Here we go and for an inventory.copy, so we'll loop through each one of those and 144 00:08:04,968 --> 00:08:08,750 then we'll choose, I'm using the up arrow here. 145 00:08:08,750 --> 00:08:11,230 We'll remove from the master list. 146 00:08:11,230 --> 00:08:14,360 So this will create a new list, we're not gonna assign it to a variable, 147 00:08:14,360 --> 00:08:16,540 it's just gonna create a new one and it's gonna loop through that. 148 00:08:16,540 --> 00:08:19,370 But we're gonna remove from the actual inventory list. 149 00:08:20,580 --> 00:08:23,490 Now if we take a look, bam. 150 00:08:23,490 --> 00:08:28,840 So just remember to be extra thoughtful when dealing with mutable objects. 151 00:08:28,840 --> 00:08:32,620 After a quick break let's head back and I wanna show off some pretty great string 152 00:08:32,620 --> 00:08:35,510 methods that are gonna help dealing with iterables like our list.