1
00:00:05,320 --> 00:00:09,100
Welcome back, everyone, to this lecture, we're going to be discussing lists in Python.

2
00:00:10,320 --> 00:00:15,540
Now, a data structure in Python that can hold elements in a sequence defined by brackets where each

3
00:00:15,540 --> 00:00:17,580
element is spaced out by a comma.

4
00:00:18,120 --> 00:00:22,110
So here, for example, we can see a list and this list has five items in it.

5
00:00:22,380 --> 00:00:24,390
The first one is a string just item.

6
00:00:24,720 --> 00:00:26,760
Next one is an integer zero.

7
00:00:26,820 --> 00:00:29,040
The third one is a floating point number ten point two.

8
00:00:29,310 --> 00:00:32,460
Fourth one is another string, and then the fifth one is actually a variable.

9
00:00:32,700 --> 00:00:34,270
We don't actually know what this variable is.

10
00:00:34,270 --> 00:00:38,580
It's just my VAR, but we can imagine it's could be a string and could be a number or something else.

11
00:00:38,820 --> 00:00:42,750
And typically, when we're organized things in a list as we get better and better, if Python, we'll

12
00:00:42,750 --> 00:00:45,300
probably just be putting a variables in there to some degree.

13
00:00:46,500 --> 00:00:52,170
Now we can use these actual lists to organize data in sequence ordering, meaning we can then grab individual

14
00:00:52,170 --> 00:00:57,570
elements from that list using the same sort of index bracket notation, as well as index slice notation

15
00:00:57,570 --> 00:00:59,160
that we learned earlier for strings.

16
00:00:59,580 --> 00:01:01,500
Let's explore the basics of Python lists.

17
00:01:02,070 --> 00:01:02,310
All right.

18
00:01:02,340 --> 00:01:04,500
Here I am inside Visual Studio Code Editor.

19
00:01:04,890 --> 00:01:09,720
If you want to create a list again, you just use square brackets and then commas to separate the items

20
00:01:09,720 --> 00:01:10,330
in the list.

21
00:01:10,410 --> 00:01:15,110
So, for example, I can say something like my list is equal to and then square brackets.

22
00:01:15,180 --> 00:01:16,500
And you can start putting in items.

23
00:01:16,860 --> 00:01:19,740
So this is a list containing three integers one two three.

24
00:01:20,160 --> 00:01:25,590
If you had another variable there or something like, let's say my variable is equal to 10, I could

25
00:01:25,800 --> 00:01:30,090
maybe add in or replace one of these with another variable like this.

26
00:01:30,510 --> 00:01:33,630
So let's go ahead and print out my list just so I can see what it looks like.

27
00:01:34,500 --> 00:01:38,100
So I'll say print my list and we should expect to see one two three 10.

28
00:01:38,550 --> 00:01:39,570
So I'll save that change.

29
00:01:40,230 --> 00:01:42,180
Let's go ahead and run Python example.

30
00:01:42,180 --> 00:01:42,940
And there I see it.

31
00:01:42,990 --> 00:01:44,100
One two three 10.

32
00:01:44,610 --> 00:01:44,910
OK.

33
00:01:45,390 --> 00:01:47,340
Now why would we want to use a list?

34
00:01:47,460 --> 00:01:53,070
A list is really useful when you have a bunch of different types of variables, like either numbers

35
00:01:53,070 --> 00:01:57,720
or strings or other variables that you want to make sure are organized in a specific order.

36
00:01:57,750 --> 00:02:02,820
That way, you can actually later on iterate through that list, or you can grab particular elements

37
00:02:02,820 --> 00:02:06,690
from that list just as we could with a string.

38
00:02:06,930 --> 00:02:09,000
So just like a string is a sequence of characters.

39
00:02:09,270 --> 00:02:11,000
A list is just a sequence of items.

40
00:02:11,009 --> 00:02:13,050
It also starts to indexing at zero.

41
00:02:13,500 --> 00:02:19,830
So to make it clear, I'm going to say my list and have it be 100, 200 and 300 as the items in the

42
00:02:19,830 --> 00:02:20,190
list.

43
00:02:20,370 --> 00:02:22,530
And let's actually go up to 500.

44
00:02:23,610 --> 00:02:24,060
There we go.

45
00:02:24,960 --> 00:02:25,920
So I'll save that change.

46
00:02:26,220 --> 00:02:28,350
Imagine I want to grab the item 100.

47
00:02:28,370 --> 00:02:29,070
How do I do that?

48
00:02:29,100 --> 00:02:30,450
I simply say my list.

49
00:02:30,870 --> 00:02:36,540
And then using the square bracket notation, just like we did four strings, I can pass on a zero,

50
00:02:36,840 --> 00:02:41,100
which says, grab these zero index item from that list.

51
00:02:41,700 --> 00:02:46,590
So I should see this print out the number 100 and there it is 100.

52
00:02:46,860 --> 00:02:51,720
Because again, all I'm saying is grab that first item at index zero.

53
00:02:52,290 --> 00:02:54,970
Now, let's imagine I actually want to take a slice.

54
00:02:55,080 --> 00:02:55,800
How does that work?

55
00:02:56,250 --> 00:02:58,070
Works the exact same way it did for strings.

56
00:02:58,080 --> 00:03:02,680
You have the starting index, then you say Kolan up to the end index.

57
00:03:02,700 --> 00:03:06,060
Imagine I wanted to grab 100, 200 and 300.

58
00:03:06,150 --> 00:03:06,750
How would that work?

59
00:03:06,960 --> 00:03:12,180
Well, I'm starting index zero and then I go up to, but not including index three.

60
00:03:12,180 --> 00:03:16,400
So it would be 100 Index zero index one index two.

61
00:03:16,410 --> 00:03:19,260
And then I wanted to go up to, but not including index three.

62
00:03:19,620 --> 00:03:26,070
So in order to get 100, 200, 300 and you say go up to index three, save that change.

63
00:03:26,580 --> 00:03:28,810
Run this and I seen 100, 200 300.

64
00:03:28,980 --> 00:03:30,090
Is a little more practice.

65
00:03:30,360 --> 00:03:33,270
Imagine I wanted to grab 200 and 300.

66
00:03:33,480 --> 00:03:34,260
How would that work?

67
00:03:34,560 --> 00:03:37,710
Well, to grab 200, I need to start an index one.

68
00:03:38,040 --> 00:03:43,410
And then to grab up to 300, I need to go up to, but not including index.

69
00:03:43,410 --> 00:03:47,820
This is index for or, excuse me, three zero one two three.

70
00:03:47,850 --> 00:03:48,140
Yup.

71
00:03:48,210 --> 00:03:52,410
The real say that and this should bring back 200 300.

72
00:03:53,370 --> 00:03:54,060
And there it is.

73
00:03:54,210 --> 00:03:58,010
If you ever confused on this, I would just encourage you to practice a lot of slicing.

74
00:03:58,020 --> 00:03:59,370
Just make yourself a really long list.

75
00:03:59,640 --> 00:04:03,360
Try to get a particular slice and see if you're indexing starts and stops makes sense.

76
00:04:03,750 --> 00:04:07,830
Now how do we add a new item to a list that already exists?

77
00:04:08,250 --> 00:04:15,180
For example, let's imagine I wanted to add in my variable into the end of this list.

78
00:04:15,240 --> 00:04:17,160
So let's go ahead and make it really obvious.

79
00:04:17,550 --> 00:04:21,850
I'm going to say new as a string, and I want to append it to the list.

80
00:04:21,870 --> 00:04:22,560
How does that work?

81
00:04:22,680 --> 00:04:24,990
This is where I get to call a method.

82
00:04:25,200 --> 00:04:30,090
So I'm going to say my list and I call the append method.

83
00:04:30,120 --> 00:04:32,910
It's one of the most common methods you call on a list.

84
00:04:33,180 --> 00:04:38,490
And this allows you to append something at the end of a list, and we'll see how to insert something

85
00:04:38,490 --> 00:04:39,780
later on anywhere in a list.

86
00:04:40,110 --> 00:04:41,640
But I can say my list append.

87
00:04:42,450 --> 00:04:46,170
Go ahead and append that variable, and then I'm going to print out.

88
00:04:47,390 --> 00:04:48,080
My list.

89
00:04:48,620 --> 00:04:51,920
And let me put this all kind of line after line here.

90
00:04:51,950 --> 00:04:56,480
So again, all I'm doing is I'm saying, go ahead and take that variable and append it to the end of

91
00:04:56,480 --> 00:04:56,960
the list.

92
00:04:57,380 --> 00:04:58,220
Save that change.

93
00:04:58,220 --> 00:05:02,990
And when you print out this list, you should now see it as one hundred or three and four to five hundred

94
00:05:02,990 --> 00:05:05,360
and then the item new has been appended to the list.

95
00:05:05,780 --> 00:05:14,060
OK, now what if you wanted to insert some item somewhere inside the list that a particular index,

96
00:05:14,300 --> 00:05:15,980
a pen will always go at the end?

97
00:05:16,340 --> 00:05:22,190
If you want to insert something at a particular index location, you can use the insert method.

98
00:05:23,770 --> 00:05:27,940
Open close parentheses and then where do you want to or what do you want to insert and where do you

99
00:05:27,940 --> 00:05:28,870
want to insert it?

100
00:05:29,350 --> 00:05:31,680
So it's actually going to take in two arguments.

101
00:05:31,690 --> 00:05:35,030
You need to know where you want to insert it and what you want to insert.

102
00:05:35,050 --> 00:05:41,380
So where just means an index location, which means this method actually takes in what is known as arguments.

103
00:05:41,770 --> 00:05:43,990
You pass in the index location, you want to insert it.

104
00:05:44,590 --> 00:05:49,390
For example, let's say I want to insert an index location zero, then I'm going to say, Comma, what

105
00:05:49,390 --> 00:05:50,380
do I want to insert?

106
00:05:51,370 --> 00:05:53,020
And I want to insert my variable here.

107
00:05:53,050 --> 00:05:57,550
I remember each time I saved the script, I am essentially kind of overwriting everything I've done

108
00:05:57,550 --> 00:06:01,630
in the past, so we shouldn't expect it to have new still at the end of that list.

109
00:06:01,900 --> 00:06:06,430
Remember, I'm essentially redefining that list again from 100 to 500 here.

110
00:06:07,150 --> 00:06:09,430
So let's save this and see how this works.

111
00:06:10,900 --> 00:06:13,510
And now I see new at the beginning of that list.

112
00:06:13,660 --> 00:06:14,590
Does that make sense?

113
00:06:14,620 --> 00:06:15,250
Hopefully it does.

114
00:06:15,250 --> 00:06:20,680
What I'm saying is go ahead and take my list insert at index location zero.

115
00:06:21,010 --> 00:06:21,940
This new variable?

116
00:06:22,420 --> 00:06:25,720
Let's go and experiment what happens if I insert it to index one?

117
00:06:27,090 --> 00:06:34,200
Then I run this the way this works is you should expect the final output result to have this variable

118
00:06:34,350 --> 00:06:35,460
at this index.

119
00:06:35,550 --> 00:06:40,620
Index one, which is true 100 Index zero now and Mew is an index one.

120
00:06:41,100 --> 00:06:46,210
So that's how you can add new items to a list append if you just want to add it to the end of the the

121
00:06:46,260 --> 00:06:46,620
list.

122
00:06:46,860 --> 00:06:51,840
That's really useful when you keep iterating and keep adding items and don't want to upend the order

123
00:06:51,840 --> 00:06:53,160
that is already in your list.

124
00:06:53,310 --> 00:06:57,840
And then you have insert when you maybe want to insert in a very specific position.

125
00:06:58,110 --> 00:07:03,440
That kind of implies that you know the list really well and know a lot of specifics about the current

126
00:07:03,510 --> 00:07:05,850
items in the list and where indices should go.

127
00:07:06,180 --> 00:07:11,430
That's why you actually don't see insert use that often compared to Append, because again, Insert

128
00:07:11,580 --> 00:07:16,200
implies, Hey, I know the list really well, and I know this very specific variable has to go to this

129
00:07:16,200 --> 00:07:18,470
very specific index versus append.

130
00:07:18,480 --> 00:07:21,330
You just say, OK, just keep adding it to the end of that list.

131
00:07:21,330 --> 00:07:26,520
And now I have kind of almost like a history of what I added to the list as I keep adding, adding more

132
00:07:26,520 --> 00:07:27,210
and more things.

133
00:07:28,140 --> 00:07:31,740
Now you may be wondering, how do I actually remove an item from a list?

134
00:07:31,890 --> 00:07:34,770
The way this works is with the pop method.

135
00:07:35,340 --> 00:07:42,840
So I'm going to say item removed as a new variable and then I'm going to set it equal to and the way

136
00:07:42,840 --> 00:07:47,460
you remove an item from a list as you pop it out, you can say my list dot pop.

137
00:07:48,120 --> 00:07:55,200
And then if you just say dot pop by itself, it's going to remove the very last item in the list.

138
00:07:55,410 --> 00:07:59,700
So what I'm going to do is, in fact, we don't actually need my variable anymore, since we won't be

139
00:07:59,700 --> 00:08:00,120
using it.

140
00:08:00,480 --> 00:08:03,630
I'm going to say my list is equal to these numbers.

141
00:08:04,140 --> 00:08:07,500
Then item removed is equal to my list dot pop.

142
00:08:07,650 --> 00:08:16,590
Notice that pop is actually going to return in item and then I'm going to print out my list and then

143
00:08:16,590 --> 00:08:19,770
I'm also going to print out the item that was removed.

144
00:08:23,530 --> 00:08:24,940
So I save those changes.

145
00:08:25,840 --> 00:08:27,670
I run this, and now I see the following.

146
00:08:28,180 --> 00:08:32,559
So I get 100, 200, 300, 400 and then I see 500.

147
00:08:32,950 --> 00:08:39,400
So by calling Pop, it removed the last item in this list and then it actually gave it to be assigned

148
00:08:39,730 --> 00:08:40,929
to this variable.

149
00:08:41,110 --> 00:08:45,940
Now, do you actually need to assign this to a variable like item removed?

150
00:08:46,180 --> 00:08:46,900
No, you do not.

151
00:08:46,960 --> 00:08:50,680
If you really wanted to, you could just say my list that pop.

152
00:08:50,860 --> 00:08:55,540
And now notice Visual Studio Code is warning us, Hey, by the way, this isn't defined anywhere.

153
00:08:55,660 --> 00:08:56,260
So that's OK.

154
00:08:56,260 --> 00:09:01,180
If I going to comment this out to put a comment in Python, it's just this hash tag symbol so you can

155
00:09:01,180 --> 00:09:02,220
put a hashtag in front of that.

156
00:09:02,230 --> 00:09:03,130
That's only a comment.

157
00:09:03,550 --> 00:09:04,720
Go ahead and save those changes.

158
00:09:05,050 --> 00:09:08,180
And what's going to happen is you will still see the actual pop occur.

159
00:09:08,200 --> 00:09:10,300
So five hundred is no longer in that list.

160
00:09:10,540 --> 00:09:13,890
You just decided not to save it to any particular variable name.

161
00:09:14,290 --> 00:09:17,080
Depending on the situation, you may just be popping things off a list.

162
00:09:17,470 --> 00:09:21,910
Now, let's imagine you want to actually remove at a specific index like index zero.

163
00:09:22,510 --> 00:09:24,440
You just pass in the index, you want to remove it.

164
00:09:24,490 --> 00:09:29,680
So if I say pop zero, that says go to index zero this list and just remove that item.

165
00:09:30,040 --> 00:09:35,500
So now when I run this, I should see 200 through 500, and there I see it 200 through 500.

166
00:09:36,850 --> 00:09:42,850
The last thing I want to point out is you do have the ability to reverse the order of a list by calling

167
00:09:42,850 --> 00:09:45,580
the reverse method, so you can say my list.

168
00:09:47,310 --> 00:09:50,040
Reverse open, close parentheses.

169
00:09:50,430 --> 00:09:52,710
And now look what's going to happen when I prints out my list.

170
00:09:53,670 --> 00:09:57,150
It is now in reverse order 500, 400, 300 200, et cetera.

171
00:09:57,540 --> 00:10:02,960
There is also the ability to sort a list depending on what items you have in your list.

172
00:10:02,970 --> 00:10:07,980
It may make sense to sort them now if you have a list of a bunch of different mixed data types like

173
00:10:07,980 --> 00:10:10,890
numbers and characters and strings and letters.

174
00:10:11,190 --> 00:10:15,600
It may not make too much sense to sort them, but if you have a bunch of numbers, then it could make

175
00:10:15,600 --> 00:10:20,160
sense to order them or sort them in ascending or descending order.

176
00:10:20,430 --> 00:10:22,950
It could also make sense to organize things in alphabetical order.

177
00:10:22,980 --> 00:10:24,360
That's pretty much the two use cases.

178
00:10:24,360 --> 00:10:29,190
You see this when you call that sort open close parentheses.

179
00:10:30,000 --> 00:10:33,570
If you're super advanced, well, maybe not super advanced, but if you're getting more advanced than

180
00:10:33,570 --> 00:10:37,680
Python, eventually you realize you can actually sort by a particular key, which means you can have

181
00:10:37,680 --> 00:10:39,780
a custom definition of how you want to sort things.

182
00:10:40,050 --> 00:10:41,450
We're just going to ignore that for right now.

183
00:10:41,460 --> 00:10:44,640
And instead, I'm going to put in a bunch of random numbers here.

184
00:10:45,180 --> 00:10:46,890
So go ahead and just kind of input.

185
00:10:46,890 --> 00:10:49,010
A bunch of random numbers doesn't matter.

186
00:10:49,020 --> 00:10:50,010
They don't need to be in order.

187
00:10:50,790 --> 00:10:54,570
And then what we're going to do here is call my list that sort.

188
00:10:54,810 --> 00:10:57,330
And then print my list and let's see the effects.

189
00:10:58,780 --> 00:11:03,970
And there you have it now, it's sorted in ascending order, starting from the east all the way to the

190
00:11:03,970 --> 00:11:04,420
greatest.

191
00:11:05,110 --> 00:11:09,100
Now the very last thing I want to point out before and this lecture, that's sometimes a common question

192
00:11:09,100 --> 00:11:12,010
is the default way of sorting is ascending.

193
00:11:12,250 --> 00:11:14,440
But what if I want things in descending order?

194
00:11:14,500 --> 00:11:15,730
How can I actually do that?

195
00:11:16,180 --> 00:11:20,320
Well, there are many different ways of doing that, but one way to combine two ideas that we already

196
00:11:20,320 --> 00:11:23,140
thought about is to actually call one method right after the other.

197
00:11:23,380 --> 00:11:30,460
I could first sort my list and then I could come here and call my list and then call reverse on it.

198
00:11:30,520 --> 00:11:34,840
So then I'm sorting in ascending order and then simply putting it in reverse.

199
00:11:34,960 --> 00:11:39,850
So I'm going from random number order, sorting it out and then reversing it, and let's go ahead and

200
00:11:39,850 --> 00:11:40,870
see the results there.

201
00:11:41,860 --> 00:11:42,540
And there you have it.

202
00:11:42,550 --> 00:11:44,590
Fifty nine and then onwards down to one.

203
00:11:45,130 --> 00:11:45,460
OK.

204
00:11:45,790 --> 00:11:47,410
That's it for the basics of lists.

205
00:11:47,440 --> 00:11:48,760
I'll see you at the next lecture.

