Welcome back.
Let's continue with lists now so far the square brackets.
We've seen them before right.
We saw them when working with strings and just like strings.
Lists are quite similar in that we can use list slicing.
If you remember with String slicing we have things like hello.
And we were able to assign into a string let's say a variable string and we could do a string and then
slice.
So do something like this where we had the start the stop and then the step through so that we start
at index of 0 and end at index of 2 and then go one by one.
So list slicing is also available to us.
So let's make this card a little bit bigger and you can actually just make things cleaner by formatting
it this way.
All right.
So what should we add to our cart.
We'll also add some toys and then you know what Amazon does groceries now so let's add some Delicious
grapes.
Awesome.
Now let's say I wanted to get every single item in the cart while we just simply do this and we have
our entire list.
But let's use some list slicing let's say I wanted to grab from the first item to the second item.
If I click Run I get notebooks and sunglasses maybe I want to go all the way till the end.
But skip every second one I get notebooks and toys.
We started zero.
We step over to toys and we step over and we're done.
Awesome.
And this is something that we've already seen when talking with strings OK but here is where it gets
interesting.
Remember how I said that strings are immutable.
That means we can't change them.
Right.
And we talked about this when we had a string like Hello.
I couldn't do greet zero equals to let's say Z.
I'd get an error here.
I get SDR object does not support item assignment.
It's immutable.
But the interesting thing with lists is that they are mutable so that if I change my Amazon cart and
say that you know what I don't really want notebooks so I'm going to grab the first item which is notebooks
and instead of notebooks.
Hi.
What do we want.
We'll say we want a new laptop.
It's a big upgrade.
When I print this Amazon cart
look at that.
I'm able to change this list.
And it didn't give me an error in that sense.
Lists are immutable.
We simply replace on the memory bookshelf of our computer notebooks.
And I say Hey change it to a laptop.
And they let us do that OK.
So that's awesome.
But let's try something here.
What if I create a now they're print here and in the Amazon cart I'll use list slicing to let's say
I want to item from index of one all the way until index of three let's run this and see what happens.
Try and guess
is that what you expected.
Let's go through this code when we get to line 10.
We grab the Amazon cart which has been updated with laptop and I grab item 1 2 3 2 that is sunglasses
to toys so 0 1 2 and then we stop at 3 just to make this easier to understand.
Let's start off with zero here so that we see that laptop has been changed and then here in the second
one online eleven we print the Amazon cart but hold on a second.
This list did not change and that is because with list slicing we're creating a new list a new copy
of this list.
So here we're creating an entirely new list so that I could actually assign it to a variable new cart
is going to be Amazon cart and let's just do the same thing here and new cart is an entirely new list
on its own.
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
two new separate lists but a list is mutable because I can change whatever is at the index anytime I
want.
And every time we do list slicing we create a new copy of that list but I have a tricky question for
you here what happens if I just do this
if I run this.
Did you see that instead of slicing my list I simply said that new cart is going to equal the Amazon
cart and I changed a new cart index of 0 2 equal to gum but now my Amazon cart got modified as well.
Why is that and this is a bit of a tricky question that you might encounter in an interview.
The reason is that right now the way that I did equals means that hey new cart is going to equal Amazon
cart.
And what does Amazon cart equal to.
Well Amazon cart points somewhere in memory in our machines that says hey this is what Amazon cart is.
So because here we're not copying the list.
Instead we're just saying hey the value of new card is whatever is in the memory of Amazon cart we now
when we modify a new cart are simply changing the Amazon cart all the way back from here.
So this is an important concept.
If you want to let's say copy a list then you do something like this where you copy the entire list
and this is something that you'll see a lot in code bases but this line is saying hey I want to create
a copy use list slicing to copy this Amazon cart and it's going to equal new cart so that now if I run
this you'll see that the original Amazon cart stays the same.
But the new cart is now something different.
This is a quick Gotcha.
They just have to get used to but it's an important concept.
This idea of copying vs. modifying the list and it's something we'll explore a little bit more in the
next couple of videos for now take a break and I'll see you in the next one by.