All right, let's learn about some actions that we can take on lists, and this is very exciting because
you can perform a lot of actions on lists.
Now, we've learned about in functions, right.
Functions that come with Python.
And we've actually seen this one before when we talked about strength's, which is the length function,
and here we can have it calculate the length of a list.
So, for example, if we had a basket here that had one, two, three, four, five.
Well, this basket, if I calculate the length of the basket, let's do print here so we can see the
result.
And I click run.
The length of the basket is five, because while there's five items, remember, a length is the actual
length, it doesn't start counting from zero.
It's going to do human length, which is five.
But lists get really powerful when it comes to methods, so instead of a built in function, remember
a method is a faction that it's owned by something and is specific, let's say, to a data type.
So if we go to list methods, you'll see that Python has a few less methods that we can use.
And the way we use.
These methods is, remember, we just add a dot.
After a list.
So let's have a look at some of them first, I'm going to start off with the adding ons, so let's say
in this basket we want to add something to it to the end of the list.
Well, we can use the append.
And remember, with a method, as soon as you write the dot, I'll tell you what we can use, which
is very, very useful when you have an editor.
So the first one is a pen.
And if I hover over this, well, it just tells me appends an object to end.
And in Python, an object is well, everything in Python is an object.
A number is an object.
A list is an object.
So just think of it as an item for now, so if I want to spend, let's say, one hundred to the end
of the list and you know what?
Let's add a new list here.
And this new list will do the append for us if I print the new list and I click run.
I get none.
Hmm, that's weird.
What if I print?
Basket here.
If I click, run.
All right, so it looks like.
The basket was a Pennetta, we added a hundred, but a new list when we assigned it the basket append
that new list is completely none and that is because append changes the list in place.
What does in place mean?
It means that it doesn't produce a value.
All it does is saying, hey, I'm just going to append a hundred to this basket that you gave me, but
I don't really care.
I'm not producing a result.
I'm just changing this for you.
I know it's a little confusing, so in order for our new list to have that one hundred at the end,
we have to do something like this.
There we go after we've appended.
To the basket, then we can assign so that new list points to Basket, which points to this list that
was modified.
All right, what else is there?
Well, there's also something called insert, and you see over here that insert gives an index and an
object so we can insert something not at the end of the list.
I mean, we could but we can also insert it anywhere we want in an index.
So, for example, in this case, if I do insert a 100.
At zero one, two, three, four, celesta index of four and I click run!
I've added 100 to the index of four, if I do index of five and I click run.
I've added one hundred at the end of the list.
Now let's try and copy this and see if we can just add it into here.
If I click, run.
Again, same thing Insert modifies the list in place, it doesn't create a new copy of the list.
All right, and if we add this.
Like this.
Once again, Insert modifies the list in place, that is, it doesn't really output a new list, it
just modifies whatever is existing in memory.
Finally, there is another method called extend and extend instead of an actual item or object takes
what we call an iterable, which we're going to get into later on.
But it's something that you can loop over.
You can iterate over, which is a list.
So we just give it another list, like, let's say one hundred or one hundred and one.
So if I run this once again, it doesn't I'll put a new list, it just modifies the list in place and
adds on.
Or extends our list and we can also just give it one item.
All right, let's keep going with the removing method's.
Now, with the removing, we once again have a few fun things that we can do.
So let's continue with this basket that we've been using that now has one hundred included into it.
And the way we can remove things is, well, there's a few ways.
First one is basket, dot, pop and basket up pop.
If I go like this and let's print this basket, I'm going to comment about the new list for now, since
we're not using it and then we're going to say basket, dot, pop and then print.
Let's run this.
And Pop pops off whatever is at the end of the list, in our case, at the end of the list, because
we've extended the basket to one hundred, that one hundred gets removed.
If I do BASKETT pop again.
Hope not here, down here and I run.
You see that both a hundred and five get removed.
What if I do pop zero?
If I click run here.
It removes the item.
In the index, so here, Pop Zero is going to remove whatever is an index of zero, which is one.
Now there's also dot remove.
Remove again is we give it a value that we want to remove.
So in our case, let's say we want to remove number four.
Well, if we run this.
It's going to remove four for us, so remove we give it the value that we want to remove with pop,
we give it the index that we want to remove.
And just to see if this is working, let's add a new list here.
And we'll say new list equals Baskette remove.
Add a new list here, see if that gets modified.
No, it does not get modified, that means remove is working in place, it doesn't return a value.
It just simply changes whatever list you give it.
What if we do, Pop?
If I run this, I get five, hmmm, why is that?
And this is something that you just have to get used to different methods, do different things.
For example, pop, the way it works is that pop returns, whatever you have just removed in our case
when we did for that is index of zero one, two, three, four eight.
Return the number five for me.
Even though it removed it from the basket, it's still returned something and the reason for others
is that we got none.
That is when a method doesn't return anything, it returns none.
A topic we're going to cover shortly.
So you have to be careful and understand what each method returns.
If I go overextend, I see here that this little arrow shows me that none is returned.
That means it's not going to produce anything with this method.
It's only going to change or extend a list that it was given.
Don't worry if this is confusing, that's something that you're going to get used to as we get more
and more practice.
Now, the last removing method I want to show you is clear and clear, as you might guess.
Fight, click, run here.
Well, this is none, but if I go to the basket and I click Run!
The basket is empty, clear, removes whatever is in the list, completely clears it.
Just as the name suggests.
All right, that was a lot, but there's still a few more, so let's take a break and I'll see you in
the next video.
Bye bye.