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 strings 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 1 2 3 4 5 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
5 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 new 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 less methods that we can use in 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 l tell you what we
can use which is very very useful when you have an editor.
So the first one is append 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 append 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.
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 a hundred but a new list when we assigned it the
basket dot append that new list is completely not.
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 100 hundred at the end we have to do something like this cyclic
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 0 1 2 3 4 so less to index a 4 and I click Run I've
added a 100 to the index of 4.
If I do index 0 5 and I click Run I've added a 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 and extend instead of an actual item or object takes what
we call an editable which we're gonna 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 a hundred or one hundred and one so if I run this once
again it doesn't I'll our 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 that way we can remove things is well.
There's a few ways.
First one is basket dot pop and basket that 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
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 100 hundred that one hundred
gets removed.
If I do basket dub pop again not here I'm down here and I run.
You see that both one hundred and five 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 1.
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
da 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 5 mm 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 0 1 2 3 4 it return the number 5 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 gonna 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 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.
I'll see you in the next video by.