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 had 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 started 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 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 gret 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 notebook's so I'm going to grab the first item,
which is notebook's and instead of notebook's 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 air in that sense, lists are immutable, 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.
OK, 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.
To 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 Kaat is an entirely new list on its own, I could change new card, 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.
Instead of slicing my list, I simply said that new card is going to equal the Amazon cart.
And I changed a new KAAT index of zero to equal to Gump, 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 card is going to equal
Amazon card.
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 Nikhat, 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.
They 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 in the next one.
Bye.