Welcome back.
In this video we're going to look at the remove keyword because we now know how to add items to the
list.
But how do we remove items from the list.
Well there is the remove method.
And therefore what I'm going to do is I'm going to use my colors list from the last video.
And I'm just going to say colors dot remove.
Okay.
So this remove method allows me to remove a particular item inside of my list.
So here I'm using the remove keyword and I'm specifically stating which item I want to be removed.
Now in this case I'm using the string red.
So it's written exactly the same way.
That's why this red will be deleted afterwards.
So let's look at our for each loop.
So I copied this for each loop.
And I pasted it in here again so that we can see the difference.
So let's run this and we will see colors in the colors list.
So red blue and green.
And then current colors in the colors list blue and green.
So we actually deleted the red.
However let's look at the example where we have colors dot add.
And then we add another red in here like so.
Now let's run the list again and see what's going to happen.
So you see red blue green red and then current colors in the list blue green red.
So what it did is it deleted the first entry of red inside of the list.
It didn't delete all red of the list, so it only deleted the first instance of red.
And that's really important to know about the remove method.
So you can hover over it.
And it says removes the first occurrence of a specific object from the list.
Okay.
And by the way it also returns something.
So if you want to store whether it could remove it or not, then you can store that inside of a bool
as it says returns true if item is successfully removed, otherwise false.
This method also returns false if item was not found in the list.
So if we, for example, would not use red but capital red and we run this again, then we see red,
blue, green, red, and then we still see red, blue, green, red.
Because the capital red.
So capital R red does not exist in the list.
So it doesn't delete it.
It doesn't know.
Well it tries to delete it, but there is no capital red in there.
So if you were to store that in a boolean let's say bool deleting successful or you could say is deleting
successful equals colors, remove red and then you print that out, or you want to see the state of
where that is.
You can just use debugging here.
So it printed all those out.
And now at this point is deleting successful is set to false.
Now if we go to the next line we will see that it's still false because it was false by default.
So when creating a bool it's false by default.
But even after we executed the line entirely, we can see that is, deleting successful is still false.
And this should be a capital D if we want to be really precise here.
So yeah, that's pretty much what you need to know about the remove method.
Now you know the workings of how to remove, but also the limitations of how to remove items from a
given list.
So now what if I wanted to delete all reds from my array?
How could I do that?
Well, pause the video and try it by yourself.
Okay, so I hope you tried it.
And this here is the solution.
Or a solution.
There are multiple ones.
Obviously I didn't want you to just run this line of code multiple times.
So colors that remove red.
I didn't want you to just write it out four times or whatever.
I wanted to make sure that you delete only as many items of red as there are, and not try to delete
more.
So what we're doing now is we're creating this boolean is deleting successful equals colors dot remove
red.
So if we can remove a red color then we're going to say is deleting successful.
So it's going to run this once again.
So first of all it's going to check okay.
Is it true.
As long as we could delete one item we're going to assume that we potentially have more items.
So we are saying is deleting successful equals colors dot remove.
So in that case we are removing one more item.
If there is one more item, if there is no more item, this boolean here will be false.
So this while loop will not run any more times.
So that's one way of doing it.
Now obviously there are other solutions to this problem.
But now let's run this code and we can see current colors in the colors list red, blue, green, red
and then current colors in the colors red list blue and green.
So what we did here is we have gotten rid of all the.
Red colors inside of our colors list.
So the idea really is that this way we are enabled to eliminate a specific item, even if it exists
multiple times.
So multiple instances of it are in the list.
We are getting rid of them.
All right.
So that's it for this video.
See you in the next one.