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