1 00:00:01,850 --> 00:00:04,590 Let's continue with lists now. 2 00:00:04,850 --> 00:00:07,820 So far, the square brackets, we've seen them before, right? 3 00:00:07,820 --> 00:00:16,700 We saw them when working with strings and just like strings lists are quite similar in that we can use 4 00:00:17,270 --> 00:00:18,860 list slicing. 5 00:00:20,150 --> 00:00:29,000 If you remember, with string slicing, we had things like hello and we were able to assign into a string, 6 00:00:29,450 --> 00:00:33,410 let's say a variable string, and we could do a string and then slice. 7 00:00:33,830 --> 00:00:35,720 So do something like this. 8 00:00:37,090 --> 00:00:45,460 Where we had the start, the stop and then the step through so that we started index of zero and end 9 00:00:45,460 --> 00:00:46,780 at index of two. 10 00:00:47,900 --> 00:00:56,420 And then go one by one, so list slicing is also available to us, so let's make this cart a little 11 00:00:56,420 --> 00:01:02,450 bit bigger and you can actually just make things cleaner by formatting it this way. 12 00:01:03,310 --> 00:01:05,650 All right, so what should we add to our cart? 13 00:01:05,920 --> 00:01:12,400 We'll also add some toys and then you know what, Amazon does groceries now. 14 00:01:12,400 --> 00:01:14,050 So let's add some delicious grapes. 15 00:01:14,970 --> 00:01:15,380 Awesome. 16 00:01:16,120 --> 00:01:21,700 Now, let's say I wanted to get every single item in the cart while we just simply do this. 17 00:01:22,670 --> 00:01:31,160 And we have our entire list, but let's use some list slicing, let's say I wanted to grab from the 18 00:01:31,160 --> 00:01:34,910 first item to the second item if I click run. 19 00:01:36,740 --> 00:01:39,050 I get notebooks and sunglasses. 20 00:01:40,240 --> 00:01:45,820 Maybe I want to go all the way till the end, but skip every second one. 21 00:01:47,670 --> 00:01:49,570 I get notebooks and toys. 22 00:01:50,340 --> 00:01:51,270 We started zero. 23 00:01:51,540 --> 00:01:55,550 We step over to toys and we step over and we're done. 24 00:01:56,280 --> 00:01:56,760 Awesome. 25 00:01:57,030 --> 00:02:00,240 And this is something that we've already seen when talking with strings. 26 00:02:01,190 --> 00:02:04,860 OK, but here is where it gets interesting. 27 00:02:05,400 --> 00:02:08,640 Remember how I said that strings are immutable. 28 00:02:08,790 --> 00:02:10,620 That means we can't change them. 29 00:02:11,100 --> 00:02:11,550 Right. 30 00:02:11,850 --> 00:02:21,420 And we talked about this when we had a string like hello, I couldn't do gret zero equals to, let's 31 00:02:21,420 --> 00:02:22,170 say Z. 32 00:02:22,650 --> 00:02:24,150 I'd get an error here. 33 00:02:24,600 --> 00:02:28,470 I get SDR object does not support item assignment. 34 00:02:29,280 --> 00:02:30,330 It's immutable. 35 00:02:31,160 --> 00:02:36,350 But the interesting thing with lists is that they are mutable. 36 00:02:37,400 --> 00:02:39,680 So that if I change my Amazon cart. 37 00:02:41,050 --> 00:02:46,390 And say that, you know what, I don't really want notebook's so I'm going to grab the first item, 38 00:02:46,390 --> 00:02:51,330 which is notebook's and instead of notebook's I what do we want? 39 00:02:51,820 --> 00:02:53,640 We'll say we want a new laptop. 40 00:02:53,860 --> 00:02:54,700 It's a big upgrade. 41 00:02:56,020 --> 00:02:58,360 When I print this Amazon cart. 42 00:03:01,370 --> 00:03:05,090 Look at that, I'm able to change this list. 43 00:03:06,740 --> 00:03:14,510 And it didn't give me an air in that sense, lists are immutable, we simply replace on the memory bookshelf 44 00:03:14,510 --> 00:03:20,030 of our computer notebooks and I say, hey, change it to laptop, and they let us do that. 45 00:03:20,920 --> 00:03:22,970 OK, so that's awesome. 46 00:03:23,480 --> 00:03:25,210 But let's try something here. 47 00:03:26,260 --> 00:03:36,430 What if I create another print here and in the Amazon cart, I'll use list slicing to let's say I want 48 00:03:36,430 --> 00:03:41,140 item from index of one all the way until index of three. 49 00:03:43,190 --> 00:03:45,060 Let's run this and see what happens. 50 00:03:45,200 --> 00:03:45,920 Try and guess. 51 00:03:49,290 --> 00:03:50,790 Is that what you expected? 52 00:03:51,600 --> 00:04:00,360 Let's go through this code when we get to line ten, we grab the Amazon cart, which has been updated 53 00:04:00,690 --> 00:04:04,050 with laptop and I grab item one. 54 00:04:05,100 --> 00:04:07,590 Two, three, so that is sunglasses. 55 00:04:08,910 --> 00:04:13,140 To toys, so zero one two, and then we stop at three. 56 00:04:14,670 --> 00:04:17,820 Just to make this easier to understand, let's start off with zero here. 57 00:04:19,030 --> 00:04:21,850 So that we see that laptop has been changed. 58 00:04:22,820 --> 00:04:28,460 And then here in the second one, on line 11, we print the Amazon cart. 59 00:04:29,420 --> 00:04:38,660 But hold on a second, this list did not change, and that is because with list slicing, we're creating 60 00:04:38,660 --> 00:04:42,430 a new list, a new copy of this list. 61 00:04:43,190 --> 00:04:49,130 So here we're creating an entirely new list so that I could actually assign it to a variable. 62 00:04:49,210 --> 00:04:52,850 New cart is going to be Amazon cart. 63 00:04:53,540 --> 00:04:55,220 And let's just do the same thing here. 64 00:04:56,750 --> 00:05:07,700 And New Kaat is an entirely new list on its own, I could change new card, let's say zero into, let's 65 00:05:07,700 --> 00:05:08,380 say gum. 66 00:05:09,290 --> 00:05:10,640 And if I print this. 67 00:05:12,490 --> 00:05:20,410 You see that I have two new separate lists, but a list is mutable because I can change whatever is 68 00:05:20,410 --> 00:05:30,010 at the index any time I want, and every time we do list slicing, we create a new copy of that list. 69 00:05:31,000 --> 00:05:36,520 But I have a tricky question for you here, what happens if I just do this? 70 00:05:39,500 --> 00:05:40,550 If I run this. 71 00:05:47,780 --> 00:05:54,470 Instead of slicing my list, I simply said that new card is going to equal the Amazon cart. 72 00:05:55,390 --> 00:06:04,210 And I changed a new KAAT index of zero to equal to Gump, but now my Amazon cart got modified as well. 73 00:06:04,930 --> 00:06:05,860 Why is that? 74 00:06:07,100 --> 00:06:12,620 And this is a bit of a tricky question that you might encounter in an interview. 75 00:06:13,630 --> 00:06:21,550 The reason is that right now, the way that I did equals means that, hey, new card is going to equal 76 00:06:21,760 --> 00:06:22,720 Amazon card. 77 00:06:23,200 --> 00:06:25,600 And what does Amazon cart equal to? 78 00:06:26,230 --> 00:06:32,680 Well, Amazon cart points somewhere in memory in our machines that says, hey, this is what Amazon 79 00:06:32,680 --> 00:06:33,340 cart is. 80 00:06:33,970 --> 00:06:36,680 So because here we're not copying the list. 81 00:06:36,700 --> 00:06:42,460 Instead, we're just saying, hey, the value of new card is whatever is in the memory of Amazon cart. 82 00:06:43,490 --> 00:06:52,140 We now, when we modify Nikhat, are simply changing the Amazon cart all the way back from here. 83 00:06:52,880 --> 00:06:54,860 So this is an important concept. 84 00:06:55,220 --> 00:07:01,100 If you want to, let's say, copy a list, then you do something like this. 85 00:07:02,260 --> 00:07:07,000 Where you copied the entire list and this is something that you'll see a lot in code bases. 86 00:07:08,060 --> 00:07:16,370 But this line is saying, hey, I want to create a copy use list slicing to copy this Amazon cart and 87 00:07:16,370 --> 00:07:20,330 it's going to equal new cart so that now if I run this. 88 00:07:22,180 --> 00:07:29,100 You'll see that the original Amazon cart stays the same, but the new cart is now something different. 89 00:07:30,100 --> 00:07:31,740 This is a quick gotcha. 90 00:07:31,750 --> 00:07:39,070 They just have to get used to, but it's an important concept, this idea of copying versus modifying 91 00:07:39,070 --> 00:07:44,260 the list, and it's something we'll explore a little bit more in the next couple of videos. 92 00:07:44,620 --> 00:07:46,920 For now, take a break and I'll see in the next one. 93 00:07:47,430 --> 00:07:47,580 Bye.