Instructor: All right.
It feels like it's never going to end.
We have so many methods with lists but,
like I said lists have a lot of methods
because you can do a lot with them.
It seems like a lot right now,
but trust me, these are going to become second nature.
So, hang in there.
I have a few more to show you.
The next one is called sort.
Guess what sort is going to do?
If I click run,
I get none.
Hmm, but remember sort, sorts the list,
as you can imagine, in place.
That means it modifies the list.
So if I remove this up to here
and then do sort or do basket,
and I click run,
look at that.
I get a sorted list where D was moved in front of E.
If I add, let's say, X at the beginning here
and I click run, it's going to move the X
all the way to the end.
It's going to sort the list in place for me.
Again, if I hover over it, it sees that it's
going to not return anything,
which is why our print initially didn't work.
We had to print our basket, not whatever this produces,
which it doesn't produce anything.
Hmm, but what about this one?
If we go to built-in functions,
I have something called sorted,
and if I do sorted,
and, because it's a function, not a method,
I can do sorted dot or basket.
So, around...
the bracket is just like print.
If I run this,
right, nothing has changed.
But what if I remove this and put it in here?
If I click run, I get A, B, C, D, D, E, X.
Hmm, what if I comment this out,
so that we're not sorting it anymore?
We just have the basket,
and then we run this sorted function.
If I run,
did you see that?
Sorted, I know it's confusing.
Why do they even have two?
But, sorted produces a new array.
So if I do print basket here and I click run,
the basket hasn't been modified.
The basket still remains the same.
It's this one over here that's not sorted.
Sorted, on the other hand, doesn't modify the basket
in place.
Instead, it creates a new copy of the basket and sorts it.
It's almost as if we're doing this.
We're saying new basket
equals basket, and then we're going to copy that basket.
Remember?
List slicing.
And then, we're performing new_basket.sort()
on this new list.
So that, if I do new basket
and I click run, we get the same thing.
So you have to be careful
of certain actions that perform an action
in place or create a completely new list to begin with.
By the way, there's also, instead of this
a dot copy method that we can use.
And if I run this again, we get the same thing.
Copy, just as the name suggests,
copy the list for us and returns for us a new list.
All right, one last one, and that is basket.reverse.
What do you think this does based on the name?
Well, if I check out the basket here and click run
is that what you expected?
It reverses the basket
in place.
But notice that it's not like sort, it's not sorting.
It's simply switching all the indexes into opposite sides.
So D is now index of zero.
E is now index of one, index of two, three,
so on and so forth.
So you can see here that it doesn't actually sort it.
Instead, while we might wanna do is sort first, then reverse
and if we click run, we have a sorted reversed basket.
This is a little tricky one
that some interviewers might ask.
All right, good job getting this far.
We're almost done with lists.
So I'll see you in the next video.
Bye-bye.