Hey guys, today I want to talk about something that is really, really important,
and you're going to use many, many times in the future,
and this is the concept of the Python List.
And the list is what you would call a Data Structure.
What does that mean?
Well, it's just a way of organizing and storing data in Python.
Now, we've already seen ways of storing single pieces of data,
and that was done through the simple variable where we just said a = 3, or
b = hello,
but that's just storing one piece of data, right?
Be it a number or a string.
But sometimes you might want to store grouped pieces of data, data that has some sort of connection
with each other.
For example, if you wanted to store all of the names of the states in the US, then it doesn't really
make sense to store them all individually because they kind of belong together, right?
They have a relationship to each other.
So it would be nice if you had a variable that was called states in the US, and then you would be able
to store all of the names of the states together in one variable.
Now, in other cases, you might also want to have order in your data.
So for example, if you were storing all of the people in a virtual queue, then you want to be able
to keep hold of the order in which they join the queue.
You don't want to let the last person somehow skip the queue because you don't have a good data structure,
right?
This is why we need to learn about lists.
And lists look pretty simple,
it's just a set of square brackets with many items stored inside, and those items can be any data type.
They can even have mixed data types like you could store strings together with numbers or a set of booleans.
It doesn't really matter, but what does matter is the syntax. In Python, lists always start with an
open square bracket like this - [, and a closing square bracket like this - ].
And then in between you have your items separated by a comma.
So pretty simple.
In order to store it inside a variable, then it's the same way as we've done before.
The only difference is the right hand side of the equal sign.
This is the list data structure.
If we stored a bunch of fruits, for example, then it might look something like this,
["Cherry", "Apple", "Pear"], separated by a comma inside a set of square brackets.
Let's take a look at this using real code.
Now let's say that I wanted to store all of the names of the states of the US. Previously, without knowing
about this list data structure, we might have written state1 = "Delaware", state2 == "Pennsylvania",
and so on and so forth.
And we would create as many variables as we have states.
But now that we know about lists, then we can just create a single variable and we call it states_of_america
.
And now we can create a list by creating a set of square brackets.
And inside those square brackets we add our items.
So again the first item is the state of Delaware which is going to be a string.
And then we've got Pennsylvania, et cetera, et cetera, and we can continue this list just by adding commas,
adding a piece of data, adding another comma, adding a piece of data.
And this way we end up with a list data structure.
Now one of the interesting things about the United States is that the different states in the US actually
joined the Union at different times.
You can actually head over to Wikipedia and watch this little animation and see each state join the
union, and at which time point they did so.
The order of this data is now kind of pretty important, because if we wanted a list of US states that
are in the order that they joined the Union, then the order in which they're stored in our data structure
is now also immensely important.
And this is another thing that you get with lists.
You can use lists to store many pieces of related data, but they also have an order.
And the order is determined by the order in the list.
So this is the first piece of data,
this is the second piece of data,
and when you store it inside the variable, that order is not lost and you'll be able to use it later
on when you need the list.
Here's a list of states of America ordered by the date that they joined the union.
And you can see that if later on I decided that I wanted to know which was the state that joined first,
then I can print this variable states_of_america, I can add a set of square brackets, and then I type
0 as the index of the piece of data I want to pull out from my states_of_america list.
So now if I go ahead and run this code, you can see it prints out Delaware.
And if I keep increasing this number, you can see that it's going through my list in the order that
it was saved.
So you might be wondering, that's kind of weird that you typed zero and you got Delaware, right?
Surely Delaware should be the first item in the list.
Well, this is a kind of peculiarity with computers and programming languages.
You'll tend to find that programmers start counting from 0.
So Delaware is at 0, Pennsylvania is at 1, and New Jersey is at 2.
And this idea might seem a little bit weird at first,
why is the first item at position 0?
But if you think about that index number, that 0, 1, or 2, instead of being the position, actually
being an offset or a shift from the start of the list,
well, then, in this case, Cherry is right at the beginning of the list, so it has an offset or a shift
of 0,
but Apple is shifted from the beginning by 1, Pear is shifted from the beginning by 2, and so
on and so forth.
Then it kind of makes more sense that the first item in the list is at the beginning of the list, so
it has no offset.
So it's 0.
And you'll find that in many, many programming languages there are similar data structures to lists
and this is how they're usually ordered starting from 0 and then adding by 1.
Now you can see that when you want to get hold of a particular piece of data stored inside a list,
what you do is you get the name of the list and then you add another set of square brackets.
So whenever you see square brackets, you should be thinking to yourself, oh, this might be related
to a list, because when you create the list, you use square brackets,
and when you try to get items out of the list, you also use square brackets.
And then inside the square brackets is where you put the index or the offset of the item that you want.
So if we wanted New Jersey, it's offset from the beginning by 1, 2.
So now this part of the code is equal to New Jersey.
Now in addition to using the positive index so say 0123 you can also use a negative index.
So if I wrote -1 or -2, then it actually starts counting from the end of the list.
So if I wrote states_of_america minus one as the index, then I get Hawaii.
Minus one is the last item in the list, because you can't really have -0.
That's not actually a real thing in maths.
Now as I continue and I go to [-2], then that's Alaska, [-3] will be Arizona and so on
and so forth.
So you can have positive indices and negative indices.
But so far we've only been pulling things out of our list by using our square brackets and the index,
but you can also change the items in the list using very similar code.
For example, if I decided that Pennsylvania is actually not spelled Pennsylvania and I wanted to change
it to Pencilvania, then I can simply write my code like this I get hold of my list, and then using
the square brackets, I get hold of the item at index 1, which is this one, and then I set it equal
to a new piece of data.
So now if I go ahead and print my states_of_america list, you'll see that the list looks a little bit
different
now.
Instead of Pennsylvania, it's now Pencilvania.
So you can alter any item inside the list pretty easily using this kind of syntax.
You can also add to the list if you wanted to.
So for example, if you wanted to add an item at the end of the list, which is what happens most commonly,
right?
If you had a list of people who are queuing in your shop, then every subsequent person usually gets
added to the end.
If you have a new state that joined America, then it's probably going to be added after Hawaii.
How do we do that?
Well, we can write the name of the list and then we use a function called append().
And append() will add a single item to the end of the list.
So let's say that Angelaland is joining the United States of America.
So now once I've appended Angelaland to the end of my states_of_america list, and I print the
states_of_america, you can see there it is added right at the end.
Now, there's actually a whole load of other functions that you can use in addition to append(),
and you'll find this on the documentation for Python.
In addition to the append() function that we saw just now, where we add an item to the end of the list,
there's a whole load of other functions that you can use with lists.
For example, you can use the extend() which adds a whole bunch of items to the end of the list.
And in this case, what you're actually adding is going to be a list.
So I would be creating the list using square brackets and then adding my items in here.
And now what's happening is I'm extending this states_of_America list with this additional list.
So now if we print the states_of_america, you can see that these two items that used to be inside a
list have now been added to the previous list, and it's now extended it by two more items.
But the important thing is you don't have to memorize these functions,
that's the whole point of documentation and why we have Google, because there's too much information
in the world for you to memorize, and it's a very inefficient way of learning.
And if you tried to memorize every single method, it's not impossible,
but it means that you don't have space in your brain for the important stuff, which is how things work.
How do you actually use it to do what you want it to do?
So what I recommend when you come across a new thing such as the list data structure, is to just
have a look through the documentation, read through it and see what are the possible things you can
do.
And once you've got the idea of this is possible, then the next time when you need to use it inside
your code, you'll know, "Uh, I remember this is possible," and all you have to do is just be able to
use Google to find the exact bit of the documentation and then implement it.
Programming is kind of like an open book exam,
you shouldn't need to memorize anything.
You should spend your time trying things out and try to get things to work instead.
Now that I've introduced you to this new data structure, the mighty List, it's time for a code exercise
to see if you can use it in practice.
So head over to the next lesson and give the challenge a go.