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