1 00:00:00,450 --> 00:00:01,859 Instructor: Welcome back. 2 00:00:01,859 --> 00:00:04,200 Let's continue with lists. 3 00:00:04,200 --> 00:00:06,540 Now, so far, these square brackets, 4 00:00:06,540 --> 00:00:07,800 we've seen them before, right? 5 00:00:07,800 --> 00:00:11,130 We saw them when working with strings 6 00:00:11,130 --> 00:00:15,060 and just like strings, lists are quite similar 7 00:00:15,060 --> 00:00:18,963 in that we can use list slicing. 8 00:00:20,190 --> 00:00:23,010 If you remember with string slicing, 9 00:00:23,010 --> 00:00:25,530 we had things like "helllooo", 10 00:00:25,530 --> 00:00:29,460 and we were able to assign it to a string, 11 00:00:29,460 --> 00:00:31,080 let's say a variable string 12 00:00:31,080 --> 00:00:33,870 and we could do a string and then slice. 13 00:00:33,870 --> 00:00:35,913 So do something like this, 14 00:00:37,110 --> 00:00:41,860 where we had the start, the stop and then the step through 15 00:00:42,720 --> 00:00:46,893 so that we start at index of zero and end at index of two, 16 00:00:47,940 --> 00:00:49,980 and then go one by one. 17 00:00:49,980 --> 00:00:53,883 So list slicing is also available to us. 18 00:00:54,840 --> 00:00:57,330 So let's make this cart a little bit bigger 19 00:00:57,330 --> 00:01:00,000 and you can actually just make things cleaner 20 00:01:00,000 --> 00:01:03,330 by formatting it this way. 21 00:01:03,330 --> 00:01:05,880 All right, so what should we add to our cart? 22 00:01:05,880 --> 00:01:09,270 We'll also add some toys. 23 00:01:09,270 --> 00:01:10,800 And then, you know what? 24 00:01:10,800 --> 00:01:12,360 Amazon does groceries now 25 00:01:12,360 --> 00:01:14,970 so let's add some delicious grapes. 26 00:01:14,970 --> 00:01:16,140 Awesome. 27 00:01:16,140 --> 00:01:18,720 Now, let's say I wanted to get every single item 28 00:01:18,720 --> 00:01:19,553 in the cart. 29 00:01:19,553 --> 00:01:24,350 Well, we just simply do this and we have our entire list. 30 00:01:25,650 --> 00:01:28,560 But let's use some list slicing. 31 00:01:28,560 --> 00:01:30,840 Let's say I wanted to grab 32 00:01:30,840 --> 00:01:34,080 from the first item to the second item. 33 00:01:34,080 --> 00:01:39,080 If I click run, I get notebooks and sunglasses. 34 00:01:40,230 --> 00:01:43,440 Maybe I wanna go all the way 'til the end 35 00:01:43,440 --> 00:01:45,933 but skip every second one. 36 00:01:47,700 --> 00:01:50,340 I get notebooks and toys. 37 00:01:50,340 --> 00:01:54,120 We start at zero, we step over to toys 38 00:01:54,120 --> 00:01:56,310 and we step over and we're done. 39 00:01:56,310 --> 00:01:58,950 Awesome, and this is something that we've already seen 40 00:01:58,950 --> 00:02:01,200 when talking with strings. 41 00:02:01,200 --> 00:02:05,400 Okay, but here is where it gets interesting. 42 00:02:05,400 --> 00:02:08,820 Remember how I said that strings are immutable? 43 00:02:08,820 --> 00:02:11,820 That means we can't change 'em, right? 44 00:02:11,820 --> 00:02:16,820 And we talked about this when we had a string like, "hello", 45 00:02:17,190 --> 00:02:22,190 I couldn't do greet[0] equals to, let's say, Z. 46 00:02:22,680 --> 00:02:24,660 I'd get an error here. 47 00:02:24,660 --> 00:02:29,310 I'd get 'str' object does not support item assignment. 48 00:02:29,310 --> 00:02:31,200 It's immutable. 49 00:02:31,200 --> 00:02:32,850 But the interesting thing 50 00:02:32,850 --> 00:02:36,483 with lists is that they are mutable. 51 00:02:37,410 --> 00:02:39,730 So that if I change my amazon_cart 52 00:02:41,070 --> 00:02:42,360 and say that, you know what? 53 00:02:42,360 --> 00:02:43,980 I don't really want notebooks 54 00:02:43,980 --> 00:02:47,580 so I'm going to grab the first item, which is notebooks. 55 00:02:47,580 --> 00:02:51,840 And instead of notebooks I, what do we want? 56 00:02:51,840 --> 00:02:53,850 We'll say we want a new laptop. 57 00:02:53,850 --> 00:02:54,843 It's a big upgrade. 58 00:02:56,070 --> 00:02:58,640 When I print this amazon_cart... 59 00:03:01,350 --> 00:03:02,183 Look at that. 60 00:03:02,183 --> 00:03:05,140 I'm able to change this list 61 00:03:06,750 --> 00:03:09,030 and it didn't give me an error. 62 00:03:09,030 --> 00:03:11,310 In that sense, lists are mutable. 63 00:03:11,310 --> 00:03:14,490 We simply replace on the memory bookshelf 64 00:03:14,490 --> 00:03:16,830 of our computer, notebooks, and I say, 65 00:03:16,830 --> 00:03:18,450 hey, change it to laptop. 66 00:03:18,450 --> 00:03:20,940 And they let us do that. 67 00:03:20,940 --> 00:03:25,323 Okay, so that's awesome, but let's try something here. 68 00:03:26,280 --> 00:03:28,980 What if I create another print here 69 00:03:28,980 --> 00:03:33,980 and in the amazon_cart I'll use list slicing 70 00:03:34,770 --> 00:03:38,910 to, let's say, I want item from index of one 71 00:03:38,910 --> 00:03:41,283 all the way until index of three. 72 00:03:43,200 --> 00:03:45,210 Let's run this and see what happens. 73 00:03:45,210 --> 00:03:46,083 Try and guess. 74 00:03:49,320 --> 00:03:51,570 Is that what you expected? 75 00:03:51,570 --> 00:03:53,610 Let's go through this code. 76 00:03:53,610 --> 00:03:58,410 When we get to line 10, we grab the amazon_cart, 77 00:03:58,410 --> 00:04:02,280 which has been updated with laptop 78 00:04:02,280 --> 00:04:05,730 and I grab item one to three. 79 00:04:05,730 --> 00:04:09,900 So that is sunglasses to toys. 80 00:04:09,900 --> 00:04:13,089 So 0, 1, 2, and then we stop at 3. 81 00:04:13,089 --> 00:04:16,200 Just to make this easier to understand, 82 00:04:16,200 --> 00:04:17,973 let's start off with zero here. 83 00:04:19,050 --> 00:04:21,993 So that we see that laptop has been changed. 84 00:04:22,890 --> 00:04:26,520 And then here in the second one, on line 11, 85 00:04:26,520 --> 00:04:27,993 we print the amazon_cart. 86 00:04:29,460 --> 00:04:31,800 But hold on a second. 87 00:04:31,800 --> 00:04:34,740 This list did not change. 88 00:04:34,740 --> 00:04:37,860 And that is because with list slicing, 89 00:04:37,860 --> 00:04:42,860 we're creating a new list, a new copy of this list. 90 00:04:43,200 --> 00:04:46,590 So here we're creating an entirely new list 91 00:04:46,590 --> 00:04:49,260 so that I could actually assign it to a variable, 92 00:04:49,260 --> 00:04:53,610 new_cart is going to be amazon_cart 93 00:04:53,610 --> 00:04:55,510 and let's just do the same thing here. 94 00:04:56,790 --> 00:05:01,790 And new_cart is an entirely new list on its own. 95 00:05:01,950 --> 00:05:06,360 I could change new_cart, let's say zero, 96 00:05:06,360 --> 00:05:08,493 into, let's say gum. 97 00:05:09,330 --> 00:05:10,833 And if I print this, 98 00:05:12,480 --> 00:05:16,740 you see that I have two new separate lists 99 00:05:16,740 --> 00:05:19,830 but a list is mutable because I can change 100 00:05:19,830 --> 00:05:23,160 whatever is at the index anytime I want. 101 00:05:23,160 --> 00:05:26,070 And every time we do list slicing, 102 00:05:26,070 --> 00:05:30,153 we create a new copy of that list. 103 00:05:31,020 --> 00:05:33,210 But I have a tricky question for you here. 104 00:05:33,210 --> 00:05:36,693 What happens if I just do this? 105 00:05:39,540 --> 00:05:40,743 If I run this? 106 00:05:43,830 --> 00:05:45,993 Hmm, did you see that? 107 00:05:47,820 --> 00:05:50,400 Instead of slicing my list, 108 00:05:50,400 --> 00:05:52,320 I simply said that new_cart is going 109 00:05:52,320 --> 00:05:57,030 to equal the amazon_cart, and I changed the new_cart, 110 00:05:57,030 --> 00:06:00,210 index of zero to equal to gum, 111 00:06:00,210 --> 00:06:04,950 but now my amazon_cart got modified as well. 112 00:06:04,950 --> 00:06:05,943 Why is that? 113 00:06:07,110 --> 00:06:10,110 And this is a bit of a tricky question 114 00:06:10,110 --> 00:06:12,723 that you might encounter in an interview. 115 00:06:13,620 --> 00:06:15,960 The reason is that right now, 116 00:06:15,960 --> 00:06:18,600 the way that I did equals means that, 117 00:06:18,600 --> 00:06:23,280 hey, new_cart is going to equal amazon_cart. 118 00:06:23,280 --> 00:06:26,250 And what does amazon_cart equal to? 119 00:06:26,250 --> 00:06:29,490 Well, amazon_cart points somewhere in memory 120 00:06:29,490 --> 00:06:33,960 in our machines that says, hey, this is what amazon_cart is. 121 00:06:33,960 --> 00:06:36,660 So because here we're not copying the list, 122 00:06:36,660 --> 00:06:38,760 instead we're just saying, hey, the value 123 00:06:38,760 --> 00:06:42,573 of new_cart is whatever is in the memory of amazon_cart, 124 00:06:43,530 --> 00:06:46,680 we now, when we modify new_cart, 125 00:06:46,680 --> 00:06:50,580 are simply changing the amazon_cart 126 00:06:50,580 --> 00:06:52,890 all the way back from here. 127 00:06:52,890 --> 00:06:55,230 So this is an important concept. 128 00:06:55,230 --> 00:06:58,860 If you want to, let's say, copy a list, 129 00:06:58,860 --> 00:07:01,293 then you do something like this, 130 00:07:02,250 --> 00:07:04,260 where you copy the entire list, 131 00:07:04,260 --> 00:07:07,160 and this is something that you'll see a lot in code bases. 132 00:07:08,040 --> 00:07:09,240 But this line is saying, 133 00:07:09,240 --> 00:07:12,360 hey, I wanna create a copy, 134 00:07:12,360 --> 00:07:16,170 use list slicing to copy this amazon_cart 135 00:07:16,170 --> 00:07:18,630 and it's going to equal new_cart, 136 00:07:18,630 --> 00:07:20,553 so that now if I run this, 137 00:07:22,230 --> 00:07:26,430 you'll see that the original amazon_cart stays the same 138 00:07:26,430 --> 00:07:29,193 but the new_cart is now something different. 139 00:07:30,090 --> 00:07:33,300 This is a quick gotcha that you just have to get used to 140 00:07:33,300 --> 00:07:34,920 but it's an important concept, 141 00:07:34,920 --> 00:07:39,920 this idea of copying versus modifying the list. 142 00:07:40,350 --> 00:07:42,030 And it's something we'll explore 143 00:07:42,030 --> 00:07:44,640 a little bit more in the next couple of videos. 144 00:07:44,640 --> 00:07:47,430 For now, take a break and I'll see you in the next one. 145 00:07:47,430 --> 00:07:48,263 Bye-bye.