-: 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 a basket
let's do print here so we can see the result.
And I click run,
I get...
five.
The length of the basket is five
because well, there's five items.
Remember a length is the actual length.
It doesn't start counting from zero.
It's gonna 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 an action
that'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 wanna 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
it'll 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 append 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 wanna 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, we'll 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 a hundred but a new list,
when we assigned it,
the basket dot 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 a hundred 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 hundred
at 0 1, 2, 3, 4, so let's do index of four,
and I click run.
I've added a hundred to the index of four.
If I do index of five and I click run
I've added a 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,
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 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 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
a 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 dot 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 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 a hundred,
that a hundred gets removed.
If I do basket dot pop again,
whoop, not here, down here,
and I run, you see that both 100 and 5 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's at index of zero, which is one.
Now, there's also dot, remove.
Remove, again, is, we give it a value
that we wanna remove.
So in our case, let's say we wanna 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 wanna remove.
With pop, we give it the index that we wanna remove.
And just to see if this is working
let's add a new list here,
and we'll say new list
equals basket dot remove.
Add a new list here.
See if that gets modified.
Nope, 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 0, 1, 2, 3, 4.
It returned 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 gonna 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 gonna get used to
as we get more and more practice.
Now, the last removing method I wanna 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's 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 then I'll see you in the next video.
Bye-bye.