1
00:00:00,160 --> 00:00:04,480
Hey guys. Today I want to talk about something that is really, really important

2
00:00:04,510 --> 00:00:07,150
and you're going to use many, many times in the future,

3
00:00:07,570 --> 00:00:10,570
and this is the concept of the Python list.

4
00:00:11,140 --> 00:00:15,520
And the list is what you would call a data structure.

5
00:00:15,880 --> 00:00:17,170
What does that mean? Well,

6
00:00:17,200 --> 00:00:21,910
it's just a way of organizing and storing data in Python.

7
00:00:22,540 --> 00:00:27,370
Now we've already seen ways of storing single pieces of data,

8
00:00:27,760 --> 00:00:32,759
and that was done through the simple variable where we just said a =

9
00:00:33,250 --> 00:00:35,680
3 or b = hello.

10
00:00:35,920 --> 00:00:40,900
But that's just storing one piece of data, right? Be it a number or a string.

11
00:00:41,650 --> 00:00:45,370
But sometimes you might want to store grouped pieces of data,

12
00:00:45,460 --> 00:00:49,690
data that has some sort of connection with each other. For example,

13
00:00:49,750 --> 00:00:54,750
if you wanted to store all of the names of the States in the US then it doesn't

14
00:00:55,240 --> 00:00:59,710
really make sense to store them all individually because they kind of belong

15
00:00:59,710 --> 00:01:02,140
together, right? They have a relationship to each other.

16
00:01:02,470 --> 00:01:06,970
So it would be nice if you had a variable that was called States_in_the_US and

17
00:01:06,970 --> 00:01:10,990
then you would be able to store all of the names of the States together in one

18
00:01:10,990 --> 00:01:13,900
variable. Now, in other cases,

19
00:01:13,960 --> 00:01:18,790
you might also want to have order in your data. So for example,

20
00:01:19,000 --> 00:01:22,930
if you were storing all of the people in a virtual queue,

21
00:01:23,230 --> 00:01:27,370
then you want to be able to keep hold of the order in which they join the queue.

22
00:01:27,760 --> 00:01:31,330
You don't wanna let the last person somehow skip the queue because you don't

23
00:01:31,330 --> 00:01:33,100
have a good data structure, right?

24
00:01:33,760 --> 00:01:38,760
This is why we need to learn about lists. And lists look pretty simple.

25
00:01:38,950 --> 00:01:43,480
It's just a set of square brackets with many items stored inside.

26
00:01:43,570 --> 00:01:47,820
And those items can be any data type. They can even have mixed data types like

27
00:01:47,820 --> 00:01:52,000
you could store strings together with numbers or a set of booleans.

28
00:01:52,330 --> 00:01:53,500
It doesn't really matter.

29
00:01:53,620 --> 00:01:58,620
But what does matter is the syntax. In Python lists always start with a open

30
00:01:59,980 --> 00:02:04,900
square bracket like this and a closing square bracket like this.

31
00:02:05,410 --> 00:02:09,009
And then in between you have your items separated by a comma.

32
00:02:09,280 --> 00:02:13,570
So pretty simple. In order to store it inside the variable,

33
00:02:13,750 --> 00:02:15,730
then its the same way as we've done before.

34
00:02:16,150 --> 00:02:19,360
The only difference is the right hand side of the equal sign.

35
00:02:19,780 --> 00:02:22,300
This is the list data structure.

36
00:02:23,170 --> 00:02:25,690
If we stored a bunch of fruits, for example,

37
00:02:25,750 --> 00:02:28,240
than it might look something like this: Cherry,

38
00:02:28,240 --> 00:02:32,410
Apple, Pear, separated by a comma inside a set of square brackets.

39
00:02:32,920 --> 00:02:35,170
Let's take a look at this using real code.

40
00:02:35,590 --> 00:02:40,300
Now I'm going to go ahead and comment out the code from our previous lesson on

41
00:02:40,330 --> 00:02:41,163
randomness

42
00:02:41,500 --> 00:02:45,790
and you can do the same if you wanna keep a note of the previous code and use it

43
00:02:45,790 --> 00:02:49,870
as sort of a live textbook where you can comment out of the code or comment it

44
00:02:49,870 --> 00:02:52,990
back in in order to see how it works or you can delete it.

45
00:02:53,140 --> 00:02:54,280
It's totally up to you.

46
00:02:55,030 --> 00:02:59,710
Now let's say that I wanted to store all of the names of the States of US.

47
00:03:00,160 --> 00:03:03,520
Previously, without knowing about this list data structure,

48
00:03:03,820 --> 00:03:07,450
we might've written state1 = Delaware,

49
00:03:08,530 --> 00:03:13,530
state2 = Pennsylvania and so on and so forth.

50
00:03:13,720 --> 00:03:16,840
And we would create as many variables as we have States.

51
00:03:17,470 --> 00:03:19,720
But now that we know about lists,

52
00:03:20,080 --> 00:03:25,080
then we can just create a single variable and we call it states_of_america.

53
00:03:26,680 --> 00:03:31,680
And now we can create a list by creating a set of square brackets and inside

54
00:03:32,230 --> 00:03:35,560
those square brackets, we add our items. So again,

55
00:03:35,620 --> 00:03:38,710
the first item is the state of Delaware,

56
00:03:38,740 --> 00:03:42,880
which is going to be a string and then we've got Pennsylvania,

57
00:03:43,570 --> 00:03:47,710
et cetera, et cetera. And we can continue this list just by adding commas,

58
00:03:48,160 --> 00:03:52,450
adding a piece of data, adding another comma, adding piece of data.

59
00:03:52,900 --> 00:03:57,430
And this way we end up with a list data structure.

60
00:03:58,390 --> 00:03:58,630
Now,

61
00:03:58,630 --> 00:04:03,630
one of the interesting things about the United States is that the different

62
00:04:03,820 --> 00:04:08,530
States in the US actually joined the union at different times.

63
00:04:08,890 --> 00:04:13,630
You can actually head over to Wikipedia and watch this little animation and see

64
00:04:13,780 --> 00:04:18,490
each state join the union and at which time point they did

65
00:04:18,490 --> 00:04:23,490
so. The order of this data is now kind of pretty important because if we wanted

66
00:04:25,960 --> 00:04:30,960
a list of US States that are in the order that they joined the union,

67
00:04:31,930 --> 00:04:36,930
then the order in which they're stored in our data structure is now also

68
00:04:37,660 --> 00:04:39,010
immensely important.

69
00:04:39,520 --> 00:04:42,700
And this is another thing that you get with lists.

70
00:04:43,120 --> 00:04:47,590
You can use a list to store many pieces of related data,

71
00:04:48,010 --> 00:04:50,770
but they also have an order.

72
00:04:50,950 --> 00:04:54,760
And the order is determined by the order in the list.

73
00:04:55,120 --> 00:04:59,050
So this is the first piece of data, this is the second piece of data.

74
00:04:59,380 --> 00:05:01,270
And when you store it inside the variable,

75
00:05:01,480 --> 00:05:06,340
that order is not lost and you'll be able to use it later on when you need the

76
00:05:06,340 --> 00:05:07,173
list.

77
00:05:07,510 --> 00:05:12,510
Here's a list of States of America ordered by the date that they joined the

78
00:05:14,110 --> 00:05:17,680
union. And you can see that if later on,

79
00:05:17,680 --> 00:05:22,420
I decided that I wanted to know which was the state that joined first,

80
00:05:22,480 --> 00:05:25,420
then I can print this variable states_of_america.

81
00:05:25,810 --> 00:05:28,270
I can add a set of square brackets,

82
00:05:28,660 --> 00:05:32,590
and then I type zero as the index of the piece of data

83
00:05:32,620 --> 00:05:35,860
I want to pull out from my states_of_america list.

84
00:05:36,250 --> 00:05:40,660
So now if I go ahead and run this code, you can see it prints out Delaware.

85
00:05:40,930 --> 00:05:43,090
And if I keep increasing this number,

86
00:05:43,390 --> 00:05:48,190
you can see that it's going through my list in the order that it was saved.

87
00:05:48,880 --> 00:05:50,770
So you might be wondering,

88
00:05:51,010 --> 00:05:55,930
that's kind of weird that you typed zero and you got Delaware,

89
00:05:55,930 --> 00:06:00,200
right? Surely, Delaware should be the first item in the list.

90
00:06:00,830 --> 00:06:05,720
Well, this is a kind of peculiarity with computers and programming languages.

91
00:06:06,080 --> 00:06:10,520
You'll tend to find that programmers start counting from zero.

92
00:06:10,700 --> 00:06:14,240
So Delaware is at zero, Pennsylvania is at one and New Jersey is at two.

93
00:06:14,870 --> 00:06:17,570
And this idea, it might seem a little bit weird at first,

94
00:06:17,600 --> 00:06:21,170
why is the first item at position 0?

95
00:06:21,860 --> 00:06:26,030
But if you think about that index number, that 0, 1 or 2,

96
00:06:26,720 --> 00:06:28,910
instead of being the position,

97
00:06:29,330 --> 00:06:34,330
actually being an offset or a shift from the start of the list.

98
00:06:35,810 --> 00:06:40,520
Well then in this case, Cherry is right at the beginning of the list,

99
00:06:40,520 --> 00:06:44,030
so it has a offset or a shift of 0.

100
00:06:44,540 --> 00:06:49,540
But Apple is shifted from the beginning by 1, Pear shifted from the beginning

101
00:06:49,790 --> 00:06:52,100
by 2 and so on and so forth.

102
00:06:52,400 --> 00:06:56,240
Then it kind of makes more sense that the first item in the list is at the

103
00:06:56,240 --> 00:06:59,480
beginning of the list. So it has no offset. So it's 0.

104
00:07:00,770 --> 00:07:03,410
And you'll find that in many, many programming languages,

105
00:07:03,680 --> 00:07:05,960
there are similar data structures to lists

106
00:07:06,230 --> 00:07:10,760
and this is how they're usually ordered starting from 0 and then adding by

107
00:07:10,760 --> 00:07:11,593
1.

108
00:07:11,900 --> 00:07:16,400
Now you can see that when you want to get hold of a particular piece of data

109
00:07:16,670 --> 00:07:18,830
stored inside a list,

110
00:07:19,190 --> 00:07:24,190
what you do is you get the name of the list and then you add another set of

111
00:07:25,340 --> 00:07:28,250
square brackets. So whenever you see square brackets,

112
00:07:28,520 --> 00:07:30,200
you should be thinking to yourself, oh,

113
00:07:30,230 --> 00:07:34,850
this might be related to a list because when you create the list,

114
00:07:34,880 --> 00:07:39,260
you use square brackets. And when you try to get items out of the list,

115
00:07:39,320 --> 00:07:40,820
you also use square brackets.

116
00:07:41,420 --> 00:07:45,860
And then inside of the square brackets is where you put the index or the offset

117
00:07:45,950 --> 00:07:49,490
of the item that you want. So if we wanted New Jersey,

118
00:07:49,490 --> 00:07:52,340
it's offset from the beginning by one two.

119
00:07:52,790 --> 00:07:56,990
So now this part of the code is equal to New Jersey.

120
00:07:57,530 --> 00:07:58,280
And we could,

121
00:07:58,280 --> 00:08:03,280
if we wanted to save it into another variable or we could simply print it as we

122
00:08:04,400 --> 00:08:06,320
did before. Now,

123
00:08:06,350 --> 00:08:09,560
in addition to using the positive index,

124
00:08:09,770 --> 00:08:12,350
so say 0, 1, 2, 3,

125
00:08:12,620 --> 00:08:14,960
you can also use a negative index.

126
00:08:15,320 --> 00:08:20,320
So if I wrote -1 or -2, then it actually starts counting from the

127
00:08:22,460 --> 00:08:26,330
end of the list. So if I wrote states_of_america

128
00:08:26,540 --> 00:08:31,010
[-1] as the index, then I get Hawaii.

129
00:08:31,430 --> 00:08:36,429
-1 is the last item in the list because you can't really have minus zero.

130
00:08:36,679 --> 00:08:40,400
That's not actually a real thing in math. Now,

131
00:08:40,460 --> 00:08:42,799
as I continue and I go to -2,

132
00:08:42,799 --> 00:08:47,780
then that's Alaska, -3 will be Arizona and so on and so forth.

133
00:08:47,840 --> 00:08:50,870
So you can have positive indices and negative indices.

134
00:08:51,440 --> 00:08:56,370
But so far we've only been pulling things out of our list by using our square

135
00:08:56,370 --> 00:08:57,840
brackets and the index.

136
00:08:58,260 --> 00:09:03,090
But you can also change the items in the list using very similar code.

137
00:09:03,480 --> 00:09:03,990
For example,

138
00:09:03,990 --> 00:09:08,990
if I decided that Pennsylvania is actually not spelled Pennsylvania and I wanted

139
00:09:10,020 --> 00:09:13,440
to change it to Pencilvania,

140
00:09:14,280 --> 00:09:18,270
then I can simply write my code like this.

141
00:09:18,810 --> 00:09:22,530
I get hold of my list. And then using the square brackets,

142
00:09:22,590 --> 00:09:25,890
I get hold of the item at index 1, which is this one.

143
00:09:26,790 --> 00:09:30,570
And then I set it equal to a new piece of data.

144
00:09:31,140 --> 00:09:36,140
So now if I go ahead and print my states_of_america list,

145
00:09:37,440 --> 00:09:40,050
you'll see that the list looks a little bit different now.

146
00:09:40,200 --> 00:09:44,250
Instead of Pennsylvania, it's now Pencilvania.

147
00:09:45,150 --> 00:09:48,090
So you can alter any item inside the list

148
00:09:48,120 --> 00:09:51,150
pretty easily using this kind of syntax.

149
00:09:51,540 --> 00:09:55,650
You could also add to the list if you wanted too. So for example,

150
00:09:55,650 --> 00:09:58,620
if you wanted to add an item at the end of the list,

151
00:09:58,650 --> 00:10:01,170
which is what happens most commonly, right?

152
00:10:01,170 --> 00:10:05,340
If you had a list of people who are cuing in your shop,

153
00:10:05,550 --> 00:10:08,790
then every subsequent person usually gets added to the end.

154
00:10:09,090 --> 00:10:12,210
If you have a new state that joined America,

155
00:10:12,480 --> 00:10:17,040
then it's probably going to be added after Hawaii. How do we do that?

156
00:10:17,370 --> 00:10:19,830
Well, we can write the name of the list

157
00:10:20,340 --> 00:10:25,340
and then we use a function called append and append

158
00:10:25,410 --> 00:10:29,130
will add a single item to the end of the list.

159
00:10:29,580 --> 00:10:34,580
So let's say that Angelaland is joining the United States of America.

160
00:10:36,600 --> 00:10:41,600
So now once I've appended Angelaland to the end of my states_of_america list,

161
00:10:42,870 --> 00:10:47,730
and I print the states_of_america, you can see, there it is added at the end.

162
00:10:48,870 --> 00:10:52,080
Now there's actually a whole load of other functions that you can use in

163
00:10:52,080 --> 00:10:56,610
addition to append. And you'll find this on the documentation for Python.

164
00:10:57,300 --> 00:11:02,250
In addition to the append function that we saw just now where we add an item at

165
00:11:02,250 --> 00:11:03,083
the end of the list,

166
00:11:03,390 --> 00:11:07,110
there's a whole load of all the functions that you can use with lists.

167
00:11:07,470 --> 00:11:09,750
For example, you can use the extend,

168
00:11:09,990 --> 00:11:14,880
which adds a whole bunch of items at the end of the list. And in this case,

169
00:11:14,910 --> 00:11:18,540
what you're actually adding is going to be a list.

170
00:11:18,900 --> 00:11:23,850
So I would be creating the list using square brackets and then adding my items

171
00:11:23,880 --> 00:11:24,713
in here.

172
00:11:25,620 --> 00:11:30,620
And now what's happening is I'm extending this states_of_america list with this

173
00:11:31,500 --> 00:11:35,640
additional list. So now if we print the states_of_america,

174
00:11:35,910 --> 00:11:40,910
you can see that these two items that used to be inside of the list have now

175
00:11:41,370 --> 00:11:44,580
been added to the previous list

176
00:11:44,910 --> 00:11:47,640
and it's now extended it by two more items.

177
00:11:48,630 --> 00:11:52,890
But the important thing is you don't have to memorize these functions.

178
00:11:52,930 --> 00:11:57,790
That's the whole point of documentation and why we have Google because there's

179
00:11:57,790 --> 00:12:00,820
too much information in the world for you to memorize.

180
00:12:01,240 --> 00:12:04,330
And it's a very inefficient way of learning.

181
00:12:04,900 --> 00:12:09,220
And if you tried to memorize every single method, it's not impossible,

182
00:12:09,220 --> 00:12:12,850
but it means that you don't have space in your brain for the important stuff,

183
00:12:12,850 --> 00:12:14,350
which is how things work.

184
00:12:14,380 --> 00:12:17,650
How do you actually use it to do what you want it to do?

185
00:12:18,070 --> 00:12:22,300
So what I recommend when you come across a new thing, such as,

186
00:12:22,600 --> 00:12:27,370
um, the list data structure is to just have a look through the documentation,

187
00:12:27,790 --> 00:12:31,690
read through it and see what are the possible things you can do.

188
00:12:32,140 --> 00:12:34,870
And once you've got the idea of this is possible,

189
00:12:35,020 --> 00:12:39,040
then the next time when you need to use it inside your code, you'll know, ah,

190
00:12:39,070 --> 00:12:41,050
I remember this is possible.

191
00:12:41,290 --> 00:12:44,290
And all you have to do is just be able to use Google,

192
00:12:44,530 --> 00:12:47,500
to find the exact bit of the documentation

193
00:12:47,770 --> 00:12:52,770
and then implement it. Programming is kind of like an open book exam.

194
00:12:53,140 --> 00:12:55,660
You shouldn't need to memorize anything.

195
00:12:55,870 --> 00:12:59,440
You should spend your time trying things out and try to get things to work

196
00:12:59,440 --> 00:13:04,330
instead. Now that I've introduced you to this new data structure,

197
00:13:04,420 --> 00:13:05,410
the mighty list,

198
00:13:05,740 --> 00:13:09,880
it's time for a code exercise to see if you can use it in practice.

199
00:13:10,210 --> 00:13:13,600
So head over to the next lesson and give the challenge that go.

