Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
Hey guys. Today I want to talk about something that is really, really important
2
and you're going to use many, many times in the future,
3
and this is the concept of the Python list.
4
And the list is what you would call a data structure.
5
What does that mean? Well,
6
it's just a way of organizing and storing data in Python.
7
Now we've already seen ways of storing single pieces of data,
8
and that was done through the simple variable where we just said a =
9
3 or b = hello.
10
But that's just storing one piece of data, right? Be it a number or a string.
11
But sometimes you might want to store grouped pieces of data,
12
data that has some sort of connection with each other. For example,
13
if you wanted to store all of the names of the States in the US then it doesn't
14
really make sense to store them all individually because they kind of belong
15
together, right? They have a relationship to each other.
16
So it would be nice if you had a variable that was called States_in_the_US and
17
then you would be able to store all of the names of the States together in one
18
variable. Now, in other cases,
19
you might also want to have order in your data. So for example,
20
if you were storing all of the people in a virtual queue,
21
then you want to be able to keep hold of the order in which they join the queue.
22
You don't wanna let the last person somehow skip the queue because you don't
23
have a good data structure, right?
24
This is why we need to learn about lists. And lists look pretty simple.
25
It's just a set of square brackets with many items stored inside.
26
And those items can be any data type. They can even have mixed data types like
27
you could store strings together with numbers or a set of booleans.
28
It doesn't really matter.
29
But what does matter is the syntax. In Python lists always start with a open
30
square bracket like this and a closing square bracket like this.
31
And then in between you have your items separated by a comma.
32
So pretty simple. In order to store it inside the variable,
33
then its the same way as we've done before.
34
The only difference is the right hand side of the equal sign.
35
This is the list data structure.
36
If we stored a bunch of fruits, for example,
37
than it might look something like this: Cherry,
38
Apple, Pear, separated by a comma inside a set of square brackets.
39
Let's take a look at this using real code.
40
Now I'm going to go ahead and comment out the code from our previous lesson on
41
randomness
42
and you can do the same if you wanna keep a note of the previous code and use it
43
as sort of a live textbook where you can comment out of the code or comment it
44
back in in order to see how it works or you can delete it.
45
It's totally up to you.
46
Now let's say that I wanted to store all of the names of the States of US.
47
Previously, without knowing about this list data structure,
48
we might've written state1 = Delaware,
49
state2 = Pennsylvania and so on and so forth.
50
And we would create as many variables as we have States.
51
But now that we know about lists,
52
then we can just create a single variable and we call it states_of_america.
53
And now we can create a list by creating a set of square brackets and inside
54
those square brackets, we add our items. So again,
55
the first item is the state of Delaware,
56
which is going to be a string and then we've got Pennsylvania,
57
et cetera, et cetera. And we can continue this list just by adding commas,
58
adding a piece of data, adding another comma, adding piece of data.
59
And this way we end up with a list data structure.
60
Now,
61
one of the interesting things about the United States is that the different
62
States in the US actually joined the union at different times.
63
You can actually head over to Wikipedia and watch this little animation and see
64
each state join the union and at which time point they did
65
so. The order of this data is now kind of pretty important because if we wanted
66
a list of US States that are in the order that they joined the union,
67
then the order in which they're stored in our data structure is now also
68
immensely important.
69
And this is another thing that you get with lists.
70
You can use a list to store many pieces of related data,
71
but they also have an order.
72
And the order is determined by the order in the list.
73
So this is the first piece of data, this is the second piece of data.
74
And when you store it inside the variable,
75
that order is not lost and you'll be able to use it later on when you need the
76
list.
77
Here's a list of States of America ordered by the date that they joined the
78
union. And you can see that if later on,
79
I decided that I wanted to know which was the state that joined first,
80
then I can print this variable states_of_america.
81
I can add a set of square brackets,
82
and then I type zero as the index of the piece of data
83
I want to pull out from my states_of_america list.
84
So now if I go ahead and run this code, you can see it prints out Delaware.
85
And if I keep increasing this number,
86
you can see that it's going through my list in the order that it was saved.
87
So you might be wondering,
88
that's kind of weird that you typed zero and you got Delaware,
89
right? Surely, Delaware should be the first item in the list.
90
Well, this is a kind of peculiarity with computers and programming languages.
91
You'll tend to find that programmers start counting from zero.
92
So Delaware is at zero, Pennsylvania is at one and New Jersey is at two.
93
And this idea, it might seem a little bit weird at first,
94
why is the first item at position 0?
95
But if you think about that index number, that 0, 1 or 2,
96
instead of being the position,
97
actually being an offset or a shift from the start of the list.
98
Well then in this case, Cherry is right at the beginning of the list,
99
so it has a offset or a shift of 0.
100
But Apple is shifted from the beginning by 1, Pear shifted from the beginning
101
by 2 and so on and so forth.
102
Then it kind of makes more sense that the first item in the list is at the
103
beginning of the list. So it has no offset. So it's 0.
104
And you'll find that in many, many programming languages,
105
there are similar data structures to lists
106
and this is how they're usually ordered starting from 0 and then adding by
107
1.
108
Now you can see that when you want to get hold of a particular piece of data
109
stored inside a list,
110
what you do is you get the name of the list and then you add another set of
111
square brackets. So whenever you see square brackets,
112
you should be thinking to yourself, oh,
113
this might be related to a list because when you create the list,
114
you use square brackets. And when you try to get items out of the list,
115
you also use square brackets.
116
And then inside of the square brackets is where you put the index or the offset
117
of the item that you want. So if we wanted New Jersey,
118
it's offset from the beginning by one two.
119
So now this part of the code is equal to New Jersey.
120
And we could,
121
if we wanted to save it into another variable or we could simply print it as we
122
did before. Now,
123
in addition to using the positive index,
124
so say 0, 1, 2, 3,
125
you can also use a negative index.
126
So if I wrote -1 or -2, then it actually starts counting from the
127
end of the list. So if I wrote states_of_america
128
[-1] as the index, then I get Hawaii.
129
-1 is the last item in the list because you can't really have minus zero.
130
That's not actually a real thing in math. Now,
131
as I continue and I go to -2,
132
then that's Alaska, -3 will be Arizona and so on and so forth.
133
So you can have positive indices and negative indices.
134
But so far we've only been pulling things out of our list by using our square
135
brackets and the index.
136
But you can also change the items in the list using very similar code.
137
For example,
138
if I decided that Pennsylvania is actually not spelled Pennsylvania and I wanted
139
to change it to Pencilvania,
140
then I can simply write my code like this.
141
I get hold of my list. And then using the square brackets,
142
I get hold of the item at index 1, which is this one.
143
And then I set it equal to a new piece of data.
144
So now if I go ahead and print my states_of_america list,
145
you'll see that the list looks a little bit different now.
146
Instead of Pennsylvania, it's now Pencilvania.
147
So you can alter any item inside the list
148
pretty easily using this kind of syntax.
149
You could also add to the list if you wanted too. So for example,
150
if you wanted to add an item at the end of the list,
151
which is what happens most commonly, right?
152
If you had a list of people who are cuing in your shop,
153
then every subsequent person usually gets added to the end.
154
If you have a new state that joined America,
155
then it's probably going to be added after Hawaii. How do we do that?
156
Well, we can write the name of the list
157
and then we use a function called append and append
158
will add a single item to the end of the list.
159
So let's say that Angelaland is joining the United States of America.
160
So now once I've appended Angelaland to the end of my states_of_america list,
161
and I print the states_of_america, you can see, there it is added at the end.
162
Now there's actually a whole load of other functions that you can use in
163
addition to append. And you'll find this on the documentation for Python.
164
In addition to the append function that we saw just now where we add an item at
165
the end of the list,
166
there's a whole load of all the functions that you can use with lists.
167
For example, you can use the extend,
168
which adds a whole bunch of items at the end of the list. And in this case,
169
what you're actually adding is going to be a list.
170
So I would be creating the list using square brackets and then adding my items
171
in here.
172
And now what's happening is I'm extending this states_of_america list with this
173
additional list. So now if we print the states_of_america,
174
you can see that these two items that used to be inside of the list have now
175
been added to the previous list
176
and it's now extended it by two more items.
177
But the important thing is you don't have to memorize these functions.
178
That's the whole point of documentation and why we have Google because there's
179
too much information in the world for you to memorize.
180
And it's a very inefficient way of learning.
181
And if you tried to memorize every single method, it's not impossible,
182
but it means that you don't have space in your brain for the important stuff,
183
which is how things work.
184
How do you actually use it to do what you want it to do?
185
So what I recommend when you come across a new thing, such as,
186
um, the list data structure is to just have a look through the documentation,
187
read through it and see what are the possible things you can do.
188
And once you've got the idea of this is possible,
189
then the next time when you need to use it inside your code, you'll know, ah,
190
I remember this is possible.
191
And all you have to do is just be able to use Google,
192
to find the exact bit of the documentation
193
and then implement it. Programming is kind of like an open book exam.
194
You shouldn't need to memorize anything.
195
You should spend your time trying things out and try to get things to work
196
instead. Now that I've introduced you to this new data structure,
197
the mighty list,
198
it's time for a code exercise to see if you can use it in practice.
199
So head over to the next lesson and give the challenge that go.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.