Welcome back in this video, we're going to look at lists and this can be used to hold both single data
types and custom objects or classes that can be resized to grow as new values are added.
The immutable types which can be written to and there are immutable types that you cannot write into
but only read from.
And let's look at those.
So first of all, let's create a list of months, so well, months, which will be a list of you just
used this method here and then you add the elements.
So for example, January, then February, and then, for example, March and so forth.
OK, I'm not going to go over all of the months.
The cool thing of list of that, it can contain any type of list item.
So while any types, for example, is going to be a list of and then you have a number, well, a bunch
of numbers here.
So one, two three and then you have the Boolean value, true or false, and then you have the string,
you know, like it's accepts all types of data.
So in order to print the amount of items that you have in an item, you can just go ahead and use print
any types thought size.
So let's run this and actually see something in action.
And this will just tell me that there are six items in here, so one, two, three, four, five and
six.
Can also access items off a given list by using the index, so for example, if I want to get February,
I would use months with the index of one.
And let me comment this line out here and run it again.
And now it will say February.
You can also look through it in a similar fashion, as we've seen before.
Right.
So you have a follow-up with the month in months and then you just print each month in a new line with
the month like so.
OK, so let's check that out, comment this line out here and get all of the months that we have in
there.
So January, February, March.
OK, so now let's look at what we can do with such a list of list.
OK, so we can just go ahead and use months dot and then see what our I.D. offers us.
And you can see it has a bunch of methods to offer.
Well, it doesn't allow me to do it properly now.
It's complaining because of the errors that we're creating here.
But you can see that there are a bunch of methods that you can use from.
It's going to allow you to select one of those.
In my case, my I.D. just doesn't seem to like it right now.
But generally speaking, you can just go ahead and check.
So here, for example, with any types thought and you can see list index of, then we have the list
iterator size.
So it has a bunch of properties as well as methods that we can use, and you could just play around
with those if you want to.
Now the problem here is that these lists are immutable, so we cannot add new items to them.
Of course, when we changed to code here, but we cannot add a new item to months, for example.
So, for example, there is no add method here, so we cannot and new items to it.
And in order to make a list that is going to be immutable, we can convert our existing list into a
mutable list and then add items.
So let's convert months into an additional month's list, and we're going to use month start to mutable
list like so and all this additional months is going to be a list now, which will be a list, but it
will be changeable so we can add new items to it.
So let's create a new month's list, which will in this case, be an array of.
And I'm just going to add April, then I'm going to add May and June, for example.
Right now, what I can do is I can combine them so I can use additional months start at all.
So I want to add all items from my new month's array.
OK, and now let's print the additional months.
Like, so let's see what's going to be displayed for us.
I could see January, February, March, April, May, June.
So we added those months and that worked.
So the month itself will only have two or three months in them.
But then we have this additional months list, which is a mutable list that we can mutate, meaning
we can change.
Now, if you want not to add all the months at once and you don't want to create a separate array off
or whatever, you can just go ahead and use additional months to add and you can add, for example,
July here.
OK, let's run the code again, and this will now add another month to our list.
You can see here so you can use it as a little challenge to add the other months that are still missing.
So positive VIDEO And at the other, either with this approach or with the approach that you saw earlier
with the array of different months.
Now, if you want to create a mutable list of a particular type, you can also go ahead and just say
mutable list off.
And then you need to define the type.
So here I'm going to find that it's going to be a type of string and here I have two different days.
So for example, Monday, Tuesday, Wednesday, OK.
And now we can add to our days because it's a mutable list, right?
We can use, add and had the first day to it and then, for example, printed days like so.
And let me not print that anymore.
Let's run it through a quick here.
And we can see it, Prince did this for us.
So that's how you add items.
What if you want to override items in a given list?
Well, you can just go ahead and say that at the position of days two, for example, which would be
Wednesday.
I'm just going to change that to Sunday.
And now that's right, again.
You can see now we have Monday, Tuesday, Sunday, Thursday.
And now, if you want to remove a given item, you can just use to remove, add method, so remove that
and then you need to pass the index, or you could remove all items, or you could remove a filter of
items and so forth.
But we're going to keep it simple.
Let's say I want to remove the item at Index three, which in this case would be thirsty.
So let's run again.
And we can see it's just as Monday, Tuesday as Sunday, so this first day that I added earlier now
was deleted.
So now instead, let's delete index one, which will delete the Tuesday from it.
So Monday, Sunday, Thursday.
OK.
That is how you can remove a particular item.
But if you want to remove all.
Well, you can just go ahead and say they start, remove all.
And here you need to say which kind of list you want to remove.
So let's say you just want to remove a filter of it.
You can create.
Another list will remove list, and this will be able to move the list or however you want to call it,
this will be a mutable list of strengths as well, but it will just contain money and wood.
OK, so now you could go ahead and only remove particular items by reusing the remove list like so and
run it again.
And you will see that Monday and Wednesday are not going to be a part of the list anymore.
So Tuesday, Sunday, Thursday, because we deleted the other list that we created.
But you can also just use days, which is also a mutable list, right?
So here let me do the same thing with day start.
Remove all, and here I'm just going to pass this.
This will remove all of the items.
So you see print dates, just give me an empty list.
So that's really how you can delete the entire thing.
Basically, you are passing the filter that you want to apply.
I am passing all day, said I want to delete, so it just removes all for me.
So I would really recommend to just go ahead and use the DOT operator to then just get some ideas of
what you can do with it.
You know, you can replace all you can add and so forth.
There are a bunch of items that you have there, like you have.
You can get the size, you can set things and so forth.
Just play around with the methods there and you should be good.