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 have things like hello and we were able to assign it to a string,
let's say a variable string and we could do 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 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.
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 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.
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 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.
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 ten, we grab the Amazon cart which has been updated with laptop and I grab item
one.
Two, three.
So that is sunglasses.
Two toys.
So, zero one, two.
And then we stop at three.
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 card 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
any time 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 card is going to equal the Amazon cart.
And I changed a 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 card 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 copied 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 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.