1
00:00:00,990 --> 00:00:06,300
Welcome back in this video, we're going to look at lists and this can be used to hold both single data

2
00:00:06,300 --> 00:00:12,930
types and custom objects or classes that can be resized to grow as new values are added.

3
00:00:13,290 --> 00:00:19,230
The immutable types which can be written to and there are immutable types that you cannot write into

4
00:00:19,440 --> 00:00:20,610
but only read from.

5
00:00:21,120 --> 00:00:22,350
And let's look at those.

6
00:00:22,470 --> 00:00:30,720
So first of all, let's create a list of months, so well, months, which will be a list of you just

7
00:00:30,840 --> 00:00:33,450
used this method here and then you add the elements.

8
00:00:33,840 --> 00:00:43,980
So for example, January, then February, and then, for example, March and so forth.

9
00:00:44,160 --> 00:00:45,990
OK, I'm not going to go over all of the months.

10
00:00:46,170 --> 00:00:49,980
The cool thing of list of that, it can contain any type of list item.

11
00:00:50,190 --> 00:00:58,020
So while any types, for example, is going to be a list of and then you have a number, well, a bunch

12
00:00:58,020 --> 00:00:58,890
of numbers here.

13
00:00:59,130 --> 00:01:07,020
So one, two three and then you have the Boolean value, true or false, and then you have the string,

14
00:01:07,770 --> 00:01:10,440
you know, like it's accepts all types of data.

15
00:01:10,860 --> 00:01:17,370
So in order to print the amount of items that you have in an item, you can just go ahead and use print

16
00:01:18,060 --> 00:01:20,340
any types thought size.

17
00:01:20,940 --> 00:01:24,510
So let's run this and actually see something in action.

18
00:01:24,870 --> 00:01:30,240
And this will just tell me that there are six items in here, so one, two, three, four, five and

19
00:01:30,240 --> 00:01:30,540
six.

20
00:01:31,910 --> 00:01:41,540
Can also access items off a given list by using the index, so for example, if I want to get February,

21
00:01:41,540 --> 00:01:44,810
I would use months with the index of one.

22
00:01:45,170 --> 00:01:47,870
And let me comment this line out here and run it again.

23
00:01:49,040 --> 00:01:50,570
And now it will say February.

24
00:01:52,710 --> 00:01:56,040
You can also look through it in a similar fashion, as we've seen before.

25
00:01:56,130 --> 00:01:56,490
Right.

26
00:01:56,670 --> 00:02:07,620
So you have a follow-up with the month in months and then you just print each month in a new line with

27
00:02:07,620 --> 00:02:08,789
the month like so.

28
00:02:09,949 --> 00:02:16,220
OK, so let's check that out, comment this line out here and get all of the months that we have in

29
00:02:16,220 --> 00:02:16,460
there.

30
00:02:16,460 --> 00:02:17,720
So January, February, March.

31
00:02:19,910 --> 00:02:25,640
OK, so now let's look at what we can do with such a list of list.

32
00:02:26,040 --> 00:02:34,160
OK, so we can just go ahead and use months dot and then see what our I.D. offers us.

33
00:02:34,400 --> 00:02:37,330
And you can see it has a bunch of methods to offer.

34
00:02:37,340 --> 00:02:40,040
Well, it doesn't allow me to do it properly now.

35
00:02:40,040 --> 00:02:43,460
It's complaining because of the errors that we're creating here.

36
00:02:43,700 --> 00:02:46,760
But you can see that there are a bunch of methods that you can use from.

37
00:02:47,360 --> 00:02:50,720
It's going to allow you to select one of those.

38
00:02:50,720 --> 00:02:54,380
In my case, my I.D. just doesn't seem to like it right now.

39
00:02:54,590 --> 00:02:57,830
But generally speaking, you can just go ahead and check.

40
00:02:58,370 --> 00:03:06,230
So here, for example, with any types thought and you can see list index of, then we have the list

41
00:03:06,230 --> 00:03:08,000
iterator size.

42
00:03:08,120 --> 00:03:14,360
So it has a bunch of properties as well as methods that we can use, and you could just play around

43
00:03:14,360 --> 00:03:15,440
with those if you want to.

44
00:03:16,070 --> 00:03:20,580
Now the problem here is that these lists are immutable, so we cannot add new items to them.

45
00:03:20,600 --> 00:03:27,110
Of course, when we changed to code here, but we cannot add a new item to months, for example.

46
00:03:27,440 --> 00:03:31,220
So, for example, there is no add method here, so we cannot and new items to it.

47
00:03:32,090 --> 00:03:39,500
And in order to make a list that is going to be immutable, we can convert our existing list into a

48
00:03:39,500 --> 00:03:41,240
mutable list and then add items.

49
00:03:41,420 --> 00:03:50,240
So let's convert months into an additional month's list, and we're going to use month start to mutable

50
00:03:50,750 --> 00:03:57,380
list like so and all this additional months is going to be a list now, which will be a list, but it

51
00:03:57,380 --> 00:03:59,840
will be changeable so we can add new items to it.

52
00:04:00,530 --> 00:04:06,590
So let's create a new month's list, which will in this case, be an array of.

53
00:04:07,280 --> 00:04:15,620
And I'm just going to add April, then I'm going to add May and June, for example.

54
00:04:15,620 --> 00:04:22,670
Right now, what I can do is I can combine them so I can use additional months start at all.

55
00:04:22,880 --> 00:04:27,620
So I want to add all items from my new month's array.

56
00:04:28,930 --> 00:04:33,160
OK, and now let's print the additional months.

57
00:04:35,200 --> 00:04:38,710
Like, so let's see what's going to be displayed for us.

58
00:04:38,740 --> 00:04:41,290
I could see January, February, March, April, May, June.

59
00:04:41,770 --> 00:04:44,440
So we added those months and that worked.

60
00:04:44,800 --> 00:04:48,490
So the month itself will only have two or three months in them.

61
00:04:48,490 --> 00:04:54,430
But then we have this additional months list, which is a mutable list that we can mutate, meaning

62
00:04:54,430 --> 00:04:55,180
we can change.

63
00:04:56,580 --> 00:05:02,760
Now, if you want not to add all the months at once and you don't want to create a separate array off

64
00:05:02,760 --> 00:05:10,440
or whatever, you can just go ahead and use additional months to add and you can add, for example,

65
00:05:11,370 --> 00:05:12,240
July here.

66
00:05:12,540 --> 00:05:17,760
OK, let's run the code again, and this will now add another month to our list.

67
00:05:17,850 --> 00:05:24,900
You can see here so you can use it as a little challenge to add the other months that are still missing.

68
00:05:25,230 --> 00:05:30,990
So positive VIDEO And at the other, either with this approach or with the approach that you saw earlier

69
00:05:30,990 --> 00:05:32,910
with the array of different months.

70
00:05:35,020 --> 00:05:42,040
Now, if you want to create a mutable list of a particular type, you can also go ahead and just say

71
00:05:42,460 --> 00:05:43,780
mutable list off.

72
00:05:44,200 --> 00:05:45,740
And then you need to define the type.

73
00:05:45,820 --> 00:05:52,000
So here I'm going to find that it's going to be a type of string and here I have two different days.

74
00:05:52,180 --> 00:05:56,080
So for example, Monday, Tuesday, Wednesday, OK.

75
00:05:56,090 --> 00:06:00,430
And now we can add to our days because it's a mutable list, right?

76
00:06:00,430 --> 00:06:07,690
We can use, add and had the first day to it and then, for example, printed days like so.

77
00:06:08,770 --> 00:06:10,990
And let me not print that anymore.

78
00:06:11,200 --> 00:06:12,670
Let's run it through a quick here.

79
00:06:14,220 --> 00:06:16,530
And we can see it, Prince did this for us.

80
00:06:18,710 --> 00:06:20,690
So that's how you add items.

81
00:06:21,080 --> 00:06:24,590
What if you want to override items in a given list?

82
00:06:25,010 --> 00:06:31,490
Well, you can just go ahead and say that at the position of days two, for example, which would be

83
00:06:31,490 --> 00:06:32,120
Wednesday.

84
00:06:32,510 --> 00:06:34,670
I'm just going to change that to Sunday.

85
00:06:35,620 --> 00:06:36,910
And now that's right, again.

86
00:06:38,130 --> 00:06:41,230
You can see now we have Monday, Tuesday, Sunday, Thursday.

87
00:06:43,750 --> 00:06:51,160
And now, if you want to remove a given item, you can just use to remove, add method, so remove that

88
00:06:52,030 --> 00:06:58,330
and then you need to pass the index, or you could remove all items, or you could remove a filter of

89
00:06:58,330 --> 00:06:59,340
items and so forth.

90
00:06:59,350 --> 00:07:00,850
But we're going to keep it simple.

91
00:07:01,120 --> 00:07:06,760
Let's say I want to remove the item at Index three, which in this case would be thirsty.

92
00:07:07,120 --> 00:07:08,230
So let's run again.

93
00:07:09,840 --> 00:07:15,090
And we can see it's just as Monday, Tuesday as Sunday, so this first day that I added earlier now

94
00:07:15,090 --> 00:07:15,810
was deleted.

95
00:07:16,080 --> 00:07:21,000
So now instead, let's delete index one, which will delete the Tuesday from it.

96
00:07:21,360 --> 00:07:22,740
So Monday, Sunday, Thursday.

97
00:07:23,790 --> 00:07:24,180
OK.

98
00:07:24,540 --> 00:07:26,760
That is how you can remove a particular item.

99
00:07:27,030 --> 00:07:29,070
But if you want to remove all.

100
00:07:29,280 --> 00:07:33,840
Well, you can just go ahead and say they start, remove all.

101
00:07:35,490 --> 00:07:39,060
And here you need to say which kind of list you want to remove.

102
00:07:39,450 --> 00:07:41,940
So let's say you just want to remove a filter of it.

103
00:07:42,450 --> 00:07:43,590
You can create.

104
00:07:44,840 --> 00:07:52,070
Another list will remove list, and this will be able to move the list or however you want to call it,

105
00:07:52,070 --> 00:07:59,380
this will be a mutable list of strengths as well, but it will just contain money and wood.

106
00:07:59,870 --> 00:08:11,120
OK, so now you could go ahead and only remove particular items by reusing the remove list like so and

107
00:08:11,120 --> 00:08:11,890
run it again.

108
00:08:11,900 --> 00:08:15,740
And you will see that Monday and Wednesday are not going to be a part of the list anymore.

109
00:08:15,740 --> 00:08:21,320
So Tuesday, Sunday, Thursday, because we deleted the other list that we created.

110
00:08:22,340 --> 00:08:27,950
But you can also just use days, which is also a mutable list, right?

111
00:08:28,640 --> 00:08:32,179
So here let me do the same thing with day start.

112
00:08:32,360 --> 00:08:36,470
Remove all, and here I'm just going to pass this.

113
00:08:36,650 --> 00:08:38,840
This will remove all of the items.

114
00:08:39,320 --> 00:08:42,890
So you see print dates, just give me an empty list.

115
00:08:43,789 --> 00:08:45,970
So that's really how you can delete the entire thing.

116
00:08:45,980 --> 00:08:49,710
Basically, you are passing the filter that you want to apply.

117
00:08:49,730 --> 00:08:55,130
I am passing all day, said I want to delete, so it just removes all for me.

118
00:08:56,820 --> 00:09:03,030
So I would really recommend to just go ahead and use the DOT operator to then just get some ideas of

119
00:09:03,030 --> 00:09:04,060
what you can do with it.

120
00:09:04,080 --> 00:09:07,200
You know, you can replace all you can add and so forth.

121
00:09:07,200 --> 00:09:11,040
There are a bunch of items that you have there, like you have.

122
00:09:11,460 --> 00:09:15,270
You can get the size, you can set things and so forth.

123
00:09:15,330 --> 00:09:18,420
Just play around with the methods there and you should be good.

