WEBVTT

0
00:00.140 --> 00:04.430
Hey guys, today I want to talk about something that is really, really important,

1
00:04.430 --> 00:07.310
and you're going to use many, many times in the future,

2
00:07.310 --> 00:10.790
and this is the concept of the Python List.

3
00:10.790 --> 00:15.620
And the list is what you would call a Data Structure.

4
00:15.620 --> 00:16.730
What does that mean?

5
00:16.730 --> 00:22.190
Well, it's just a way of organizing and storing data in Python.

6
00:22.190 --> 00:27.560
Now, we've already seen ways of storing single pieces of data,

7
00:27.560 --> 00:35.120
and that was done through the simple variable where we just said a = 3, or

8
00:35.120 --> 00:35.690
b = hello,

9
00:35.690 --> 00:38.720
but that's just storing one piece of data, right?

10
00:38.720 --> 00:41.090
Be it a number or a string.

11
00:41.510 --> 00:47.510
But sometimes you might want to store grouped pieces of data, data that has some sort of connection

12
00:47.510 --> 00:48.530
with each other.

13
00:48.530 --> 00:55.680
For example, if you wanted to store all of the names of the states in the US, then it doesn't really

14
00:55.680 --> 01:00.390
make sense to store them all individually because they kind of belong together, right?

15
01:00.390 --> 01:02.250
They have a relationship to each other.

16
01:02.250 --> 01:07.560
So it would be nice if you had a variable that was called states in the US, and then you would be able

17
01:07.560 --> 01:11.820
to store all of the names of the states together in one variable.

18
01:12.060 --> 01:17.460
Now, in other cases, you might also want to have order in your data.

19
01:17.460 --> 01:24.480
So for example, if you were storing all of the people in a virtual queue, then you want to be able

20
01:24.480 --> 01:27.480
to keep hold of the order in which they join the queue.

21
01:27.510 --> 01:32.730
You don't want to let the last person somehow skip the queue because you don't have a good data structure,

22
01:32.730 --> 01:33.390
right?

23
01:33.420 --> 01:36.150
This is why we need to learn about lists.

24
01:36.150 --> 01:38.940
And lists look pretty simple,

25
01:38.940 --> 01:46.080
it's just a set of square brackets with many items stored inside, and those items can be any data type.

26
01:46.080 --> 01:52.140
They can even have mixed data types like you could store strings together with numbers or a set of booleans.

27
01:52.140 --> 01:59.610
It doesn't really matter, but what does matter is the syntax. In Python, lists always start with an

28
01:59.610 --> 02:05.100
open square bracket like this - [, and a closing square bracket like this - ].

29
02:05.100 --> 02:09.090
And then in between you have your items separated by a comma.

30
02:09.090 --> 02:10.590
So pretty simple.

31
02:10.890 --> 02:15.840
In order to store it inside a variable, then it's the same way as we've done before.

32
02:15.870 --> 02:19.500
The only difference is the right hand side of the equal sign.

33
02:19.500 --> 02:22.530
This is the list data structure.

34
02:22.890 --> 02:27.720
If we stored a bunch of fruits, for example, then it might look something like this,

35
02:27.720 --> 02:32.560
["Cherry", "Apple", "Pear"], separated by a comma inside a set of square brackets.

36
02:32.830 --> 02:35.260
Let's take a look at this using real code.

37
02:35.290 --> 02:42.130
Now let's say that I wanted to store all of the names of the states of the US. Previously, without knowing

38
02:42.130 --> 02:50.980
about this list data structure, we might have written state1 = "Delaware", state2 == "Pennsylvania",

39
02:50.980 --> 02:54.190
and so on and so forth.

40
02:54.190 --> 02:57.610
And we would create as many variables as we have states.

41
02:57.730 --> 03:05.590
But now that we know about lists, then we can just create a single variable and we call it states_of_america

42
03:05.590 --> 03:06.490
.

43
03:06.910 --> 03:11.380
And now we can create a list by creating a set of square brackets.

44
03:11.380 --> 03:15.190
And inside those square brackets we add our items.

45
03:15.190 --> 03:21.100
So again the first item is the state of Delaware which is going to be a string.

46
03:21.580 --> 03:28.450
And then we've got Pennsylvania, et cetera, et cetera, and we can continue this list just by adding commas,

47
03:28.450 --> 03:33.160
adding a piece of data, adding another comma, adding a piece of data.

48
03:33.160 --> 03:38.110
And this way we end up with a list data structure.

49
03:38.800 --> 03:46.690
Now one of the interesting things about the United States is that the different states in the US actually

50
03:46.690 --> 03:49.240
joined the Union at different times.

51
03:49.240 --> 03:55.900
You can actually head over to Wikipedia and watch this little animation and see each state join the

52
03:55.900 --> 03:59.590
union, and at which time point they did so.

53
03:59.650 --> 04:08.320
The order of this data is now kind of pretty important, because if we wanted a list of US states that

54
04:08.320 --> 04:16.610
are in the order that they joined the Union, then the order in which they're stored in our data structure

55
04:16.610 --> 04:19.760
is now also immensely important.

56
04:19.760 --> 04:23.390
And this is another thing that you get with lists.

57
04:23.390 --> 04:31.280
You can use lists to store many pieces of related data, but they also have an order.

58
04:31.280 --> 04:35.390
And the order is determined by the order in the list.

59
04:35.390 --> 04:37.670
So this is the first piece of data,

60
04:37.700 --> 04:39.650
this is the second piece of data,

61
04:39.650 --> 04:45.560
and when you store it inside the variable, that order is not lost and you'll be able to use it later

62
04:45.560 --> 04:47.360
on when you need the list.

63
04:47.960 --> 04:55.340
Here's a list of states of America ordered by the date that they joined the union.

64
04:55.340 --> 05:02.910
And you can see that if later on I decided that I wanted to know which was the state that joined first,

65
05:02.910 --> 05:09.810
then I can print this variable states_of_america, I can add a set of square brackets, and then I type

66
05:09.810 --> 05:16.530
0 as the index of the piece of data I want to pull out from my states_of_america list.

67
05:16.530 --> 05:21.240
So now if I go ahead and run this code, you can see it prints out Delaware.

68
05:21.240 --> 05:27.950
And if I keep increasing this number, you can see that it's going through my list in the order that

69
05:27.950 --> 05:28.940
it was saved.

70
05:29.090 --> 05:36.890
So you might be wondering, that's kind of weird that you typed zero and you got Delaware, right?

71
05:36.890 --> 05:41.030
Surely Delaware should be the first item in the list.

72
05:41.030 --> 05:46.400
Well, this is a kind of peculiarity with computers and programming languages.

73
05:46.400 --> 05:51.120
You'll tend to find that programmers start counting from 0.

74
05:51.120 --> 05:54.990
So Delaware is at 0, Pennsylvania is at 1, and New Jersey is at 2.

75
05:55.020 --> 05:58.080
And this idea might seem a little bit weird at first,

76
05:58.080 --> 06:01.980
why is the first item at position 0?

77
06:02.130 --> 06:10.200
But if you think about that index number, that 0, 1, or 2, instead of being the position, actually

78
06:10.200 --> 06:15.990
being an offset or a shift from the start of the list,

79
06:16.080 --> 06:23.760
well, then, in this case, Cherry is right at the beginning of the list, so it has an offset or a shift

80
06:23.760 --> 06:24.750
of 0,

81
06:24.750 --> 06:31.710
but Apple is shifted from the beginning by 1, Pear is shifted from the beginning by 2, and so

82
06:31.710 --> 06:32.730
on and so forth.

83
06:32.730 --> 06:38.010
Then it kind of makes more sense that the first item in the list is at the beginning of the list, so

84
06:38.010 --> 06:38.880
it has no offset.

85
06:38.880 --> 06:40.140
So it's 0.

86
06:40.980 --> 06:46.530
And you'll find that in many, many programming languages there are similar data structures to lists

87
06:46.530 --> 06:51.900
and this is how they're usually ordered starting from 0 and then adding by 1.

88
06:52.170 --> 06:59.460
Now you can see that when you want to get hold of a particular piece of data stored inside a list,

89
06:59.460 --> 07:06.720
what you do is you get the name of the list and then you add another set of square brackets.

90
07:06.720 --> 07:11.850
So whenever you see square brackets, you should be thinking to yourself, oh, this might be related

91
07:11.850 --> 07:16.800
to a list, because when you create the list, you use square brackets,

92
07:16.800 --> 07:21.570
and when you try to get items out of the list, you also use square brackets.

93
07:21.570 --> 07:27.790
And then inside the square brackets is where you put the index or the offset of the item that you want.

94
07:27.820 --> 07:33.010
So if we wanted New Jersey, it's offset from the beginning by 1, 2.

95
07:33.040 --> 07:37.660
So now this part of the code is equal to New Jersey.

96
07:37.690 --> 07:46.900
Now in addition to using the positive index so say 0123 you can also use a negative index.

97
07:46.900 --> 07:55.600
So if I wrote -1 or -2, then it actually starts counting from the end of the list.

98
07:55.600 --> 08:03.040
So if I wrote states_of_america minus one as the index, then I get Hawaii.

99
08:03.070 --> 08:08.500
Minus one is the last item in the list, because you can't really have -0.

100
08:08.500 --> 08:10.930
That's not actually a real thing in maths.

101
08:11.650 --> 08:18.970
Now as I continue and I go to [-2], then that's Alaska, [-3] will be Arizona and so on

102
08:18.970 --> 08:19.660
and so forth.

103
08:19.660 --> 08:22.930
So you can have positive indices and negative indices.

104
08:22.930 --> 08:29.800
But so far we've only been pulling things out of our list by using our square brackets and the index,

105
08:29.800 --> 08:35.110
but you can also change the items in the list using very similar code.

106
08:35.110 --> 08:42.610
For example, if I decided that Pennsylvania is actually not spelled Pennsylvania and I wanted to change

107
08:42.610 --> 08:53.440
it to Pencilvania, then I can simply write my code like this I get hold of my list, and then using

108
08:53.440 --> 09:00.730
the square brackets, I get hold of the item at index 1, which is this one, and then I set it equal

109
09:00.730 --> 09:02.710
to a new piece of data.

110
09:02.710 --> 09:11.180
So now if I go ahead and print my states_of_america list, you'll see that the list looks a little bit

111
09:11.180 --> 09:11.510
different

112
09:11.540 --> 09:11.960
now.

113
09:11.960 --> 09:16.310
Instead of Pennsylvania, it's now Pencilvania.

114
09:16.760 --> 09:23.180
So you can alter any item inside the list pretty easily using this kind of syntax.

115
09:23.180 --> 09:26.300
You can also add to the list if you wanted to.

116
09:26.330 --> 09:32.720
So for example, if you wanted to add an item at the end of the list, which is what happens most commonly,

117
09:32.720 --> 09:32.990
right?

118
09:32.990 --> 09:39.770
If you had a list of people who are queuing in your shop, then every subsequent person usually gets

119
09:39.770 --> 09:40.730
added to the end.

120
09:40.730 --> 09:47.390
If you have a new state that joined America, then it's probably going to be added after Hawaii.

121
09:47.720 --> 09:49.040
How do we do that?

122
09:49.040 --> 09:55.860
Well, we can write the name of the list and then we use a function called append().

123
09:55.860 --> 10:01.170
And append() will add a single item to the end of the list.

124
10:01.170 --> 10:07.980
So let's say that Angelaland is joining the United States of America.

125
10:08.190 --> 10:16.320
So now once I've appended Angelaland to the end of my states_of_america list, and I print the

126
10:16.320 --> 10:19.730
states_of_america, you can see there it is added right at the end.

127
10:20.510 --> 10:25.310
Now, there's actually a whole load of other functions that you can use in addition to append(),

128
10:25.310 --> 10:28.730
and you'll find this on the documentation for Python.

129
10:28.790 --> 10:35.000
In addition to the append() function that we saw just now, where we add an item to the end of the list,

130
10:35.000 --> 10:39.080
there's a whole load of other functions that you can use with lists.

131
10:39.080 --> 10:45.040
For example, you can use the extend() which adds a whole bunch of items to the end of the list.

132
10:45.610 --> 10:50.560
And in this case, what you're actually adding is going to be a list.

133
10:50.560 --> 10:56.560
So I would be creating the list using square brackets and then adding my items in here.

134
10:57.370 --> 11:04.810
And now what's happening is I'm extending this states_of_America list with this additional list.

135
11:04.810 --> 11:12.090
So now if we print the states_of_america, you can see that these two items that used to be inside a

136
11:12.090 --> 11:19.620
list have now been added to the previous list, and it's now extended it by two more items.

137
11:20.250 --> 11:24.720
But the important thing is you don't have to memorize these functions,

138
11:24.720 --> 11:30.810
that's the whole point of documentation and why we have Google, because there's too much information

139
11:30.810 --> 11:36.420
in the world for you to memorize, and it's a very inefficient way of learning.

140
11:36.420 --> 11:41.040
And if you tried to memorize every single method, it's not impossible,

141
11:41.040 --> 11:46.260
but it means that you don't have space in your brain for the important stuff, which is how things work.

142
11:46.260 --> 11:49.650
How do you actually use it to do what you want it to do?

143
11:49.650 --> 11:57.360
So what I recommend when you come across a new thing such as the list data structure, is to just

144
11:57.360 --> 12:03.210
have a look through the documentation, read through it and see what are the possible things you can

145
12:03.210 --> 12:03.720
do.

146
12:03.720 --> 12:09.270
And once you've got the idea of this is possible, then the next time when you need to use it inside

147
12:09.270 --> 12:15.390
your code, you'll know, "Uh, I remember this is possible," and all you have to do is just be able to

148
12:15.390 --> 12:20.860
use Google to find the exact bit of the documentation and then implement it.

149
12:21.730 --> 12:24.850
Programming is kind of like an open book exam,

150
12:24.850 --> 12:27.580
you shouldn't need to memorize anything.

151
12:27.580 --> 12:32.050
You should spend your time trying things out and try to get things to work instead.

152
12:32.200 --> 12:39.700
Now that I've introduced you to this new data structure, the mighty List, it's time for a code exercise

153
12:39.700 --> 12:41.860
to see if you can use it in practice.

154
12:41.860 --> 12:45.640
So head over to the next lesson and give the challenge a go.