Instructor: Welcome back.
Let's continue with lists.
Now, so far, these 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 had things like "helllooo",
and we were able to assign it to 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 zero and end at index of two,
and then go one by one.
So list slicing is also available to us.
So let's make this cart 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.
Well, 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 wanna go all the way 'til the end
but skip every second one.
I get notebooks and toys.
We start at 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.
Okay, but here is where it gets interesting.
Remember how I said that strings are immutable?
That means we can't change 'em, right?
And we talked about this when we had a string like, "hello",
I couldn't do greet[0] equals to, let's say, Z.
I'd get an error here.
I'd get 'str' 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 I, 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 mutable.
We simply replace on the memory bookshelf
of our computer, notebooks, and I say,
hey, change it to laptop.
And they let us do that.
Okay, so that's awesome, but let's try something here.
What if I create another print here
and in the amazon_cart I'll use list slicing
to, let's say, I want 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 one to three.
So 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, on line 11,
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 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?
Hmm, 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 the new_cart,
index of zero to 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_cart is whatever is in the memory of amazon_cart,
we now, when we modify 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 wanna 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 that you just have to get used to
but it's an important concept,
this idea of copying versus 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.
Bye-bye.