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 built in functions, right?
Functions that come with Python.
And we've actually seen this one before when we talked about strengths, 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.
I get.
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 nice action that it's owned by something
and it's specific, let's say, to a data type.
So if we go to list methods, you'll see that Python has a few list 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 ones.
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 a pen is 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 append, let's say 100 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 appended to we added 100 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 100 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 100 at the end, we have to do something like this.
If I click Run.
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.
So let's do 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 100 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.
Well.
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.
An 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 100 or 101.
So if I run this once again, it doesn't output 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 methods.
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 100 included into it.
And the way we can remove things is, well, there's a few ways.
First one is basket dot pop.
And Basketball Pop.
If I go like this and let's print this basket, I'm going to comment out the new list for now since
we're not using it.
And then we're going to say basket, dot, pop and then print.
Basket.
Let's run this.
And Pop Pop's 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 100.
That 100 gets removed.
If I do basket dot pop again.
Oop.
Not here.
Down here.
And I run.
You see that both 105 get removed?
What if I do pop zero?
Five Click Run here.
It removes the item.
In the index.
So here, Pop zero is going to remove whatever is that 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 basket 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.
Hmm.
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 four, that is index of zero one, two, three, four it return the number
five for me.
Even though it removed it from the basket, it 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 over extend.
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, if I click run here.
Well, this is none.
But if I go to Basket and I click Run.
The basket is empty.
Clear removes whatever is in the list.
Completely clears it.
Just as a 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.