Let's take a formal look at lists.
So lists are, and we've covered this before, but it's like
a grocery list.
So you go for 'Milk', 'Bread', and 'Eggs', and you have 'Milk', 'Bread', and 'Eggs'.
But in other programming languages they aren't called lists.
They are called arrays, which are a lot more common.
Python just uses its own terminology for some reason. But
lists and arrays are essentially the same thing in most programming
languages. So let's compare the difference between a list and a string.
So we've got a 'lst =', and it can take any number
of items, and it will preserve the order.
So it'll always be [1, 2, 3, 4, 5] here,
whereas a string can only ever have one value, 'st = "One value to
rule them all".
And a number or an integer can only ever be like 1 or 1.0.
So a list is basically a bunch of strings, a bunch of numbers,
a bunch of Booleans, all these different data types,
you can put them into a list together.
You can have lists inside of lists as well if you wanted
to. So let's go ahead and take a look at the syntax of a
list. 'my_friends'. Let's do 'my_cats'.
So we've got a list here, and the syntax is basically the
coding language, how this looks.
So I've got a variable, standard variable, is equal to,
so I'm assigning something,
and this is how we create a new list in Python.
In Python, this is just going to say, "Hey, there's a piece of
memory, and it should be code named 'my_cats'."
In here, I'm going to put my first item called 'Henry', and
I'll put another one in here called 'Zephyr'.
It starts with a hard bracket, ends with a hard bracket, '[]'
These are strings, so these have to have quotations or apostrophes
around them. And then between each item is simply a comma.
The space is not really important, but if you add a space,
it just seems to look a little nicer.
So let's go ahead and see what we have here.
Python thinks that 'my_cats' are '['Henry', 'Zephyr']'.
And because it's hard brackets, you can immediately tell that
that is a list.
Also, you have two items here, so it looks like you've got
one string, comma, another string.
Just by reading that, you could figure out that it's not
just a string, it's not just a number, it's a list.
So when it comes to data science, then this may or may not
be applicable to you, but in data science, you are going to
see a lot of different lists.
You're actually going to see lists of lists, which look crazy,
but these build out to these things called matrices or matrices.
So in here we've got an opening list, '[', and then we've got
'1, 2, 3]
','; we've got another list in here, '[4, 5, 6]'.
Let's add spaces, though,
and '[7, 8, 9]'.
And so when you look at this, this actually does sort of look like a grid.
We've got a list of lists in here, and so when we loop through
every item, we're going to get list number one, list number
two, list number three.
And then inside of each one of those, we would have [1, 2, 3];
in the second one we'll have [4, 5, 6]; and the third one we'll have [7, 8, 9].
Now, that's neither here nor there, and when you flatten
it, it looks like this. It looks a little harder to read when it's flat.
But in data science, you're going to see something like this
fairly frequently.
Now, if you're not going into the data science route, that's
okay. It's still good to know that list can be nested inside of another list.
Now, just going back a quick second here, I've got a variable.
It's a list called 'Henry', a list called 'my_cats', with items
called ['Henry', 'Zephyr'] in it.
Now, lists are not quite the same as strings.
They don't have all the same methods attached to it.
If we do 'type(my_cats)', we're going to see that it's a Python
'list'. 'my_cats.', and then hit tab,
you're going to see all these different methods, things that
we can do here. So I can 'append',
I can 'clear', I can 'copy',
I can 'count' the number of times something shows up,
I can 'extend' it, 'index', 'insert' into it, 'pop', which is remove the last item,
I can 'remove' something, I can 'reverse' it, and I can 'sort'
it. All of these are pretty self explanatory for the most part.
So if we just run 'reverse', and do 'my_cats', it's now
the opposite.
So it was originally '['Henry', 'Zephyr']',
It is now '['Zephyr', 'Henry']'.
But let's go ahead and do some actual Python coding
or something that's going to be actually useful.
So one thing we're going to do a lot as a Python developer,
Python programmer, is we're going to add to our list, to my
list has Henry and Zephyr in it, Zephyr and Henry in that proper
order, and I'm going to append to it.
Now, if you come from a JavaScript background that's called
Push, in Python it's 'append', and I'm going to add a name in here.
"Ezra". Now, it looks like nothing happened, but if I put 'my_cats'
in here, you can see that it actually changed my variable,
it added more to it.
We didn't have to overwrite it, so we didn't have to do
'my_cats = my_cats.append(
"Ezra")'. We just had to do 'my_cats.append', and it automatically
updated its internal listings, or its internal values for
us. Now, let's say we wanted to remove "Henry".
Not that I would ever want to, but let's say, just for funsies
I wanted to remove "Henry",
I could do 'my_cats',
and the way I got 'remove' by the way, is just 'my_cats',
that's the list,
'.', and then 'remove',
and let's remove the one item in here called 'Henry'.
Again, it looks like nothing happened.
But behind the scenes, Python said, "Okay, I understand you
wanted to get rid of 'Henry'.
That's what I did".
Now, what do you think would happen if I said that some 'value
= my_cats.append("Henry")'
So we're going to put 'Henry' back in the list, right? Before
we weren't doing 'value =', or variable is equal to,
let's call it 'var'.
'var = my_cats',
we weren't doing that.
We were just simply saying, 'my_cats.append', and then we put
something in there, but this time we're not doing that.
Okay, looks like everything's fine.
And when we check out 'my_cats', 'my_cats' is going to be totally
fine. It's going to do exactly what we said.
It added 'Henry' in there.
But what is 'var' going to be if 'my_cats' is already changed?
Is this, whole this, now going to be in 'var'?
Let's take a look at this.
There's actually nothing in there.
Let's take a look at the 'type(var)'.
'None'. Now, that is another data type that we will eventually get
into, but 'None' in Python means null.
It's void. There's nothing in there.
And so when we're running methods on a list like this, like
'.append', or like, '.remove', we don't need to assign this variable.
It's not useful.
It's already going to update its internal workings of how
this list works, and it's going to return absolutely nothing
or a 'NoneType',
and it's going to put that into the variable that we called
'var'. So it's not useful.
So you don't ever need to do that with a list.
You can always just work directly on the list itself.
Now, the reason I bring that up is because quite a while
ago we said something like, 'name = "Kalob", and then
we overwrote it with 'name = "Zephyr"'.
Well, when it comes to list, we don't need to do that unless
we want to replace the entire thing.
So we could say 'name = [1, 2, 3]', and this
is going to overwrite it with [4, 5, 6].
And then let's 'print(name)', and I'll move that up as well.
We print the 'name'. It was assigned, but then was immediately
reassigned. That's sort of what we did quite a while ago.
And I'm just showing you that we don't need to do that when
it comes to lists, unless you want to replace the entire thing.
Now, one last thing I want to show you is just to make this
a little more confusing,
because I'm sure this is totally not super confusing right now,
but it does get easier over time, is if we look at
'my_cats' once more,
we have '['Henry', 'Ezra', 'Zephyr']', not in that order.
We have '['Zephyr', 'Ezra', 'Henry']', and let's say I just wanted to
get just the last item.
Now, when you're just learning to program, you're probably
thinking, "Well, that's not going to be useful.
Why would I ever want just the last item?"
But there are going to be a lot of cases down the road where
you do just want the last item.
So what we can do is a thing called 'pop', and we can say 'henry
= my_cats.pop()'.
Now, this method is interesting.
It's going to take this value, and is going to pull it out
of that list, update the list,
so it's just ['Zephyr', 'Ezra']', and is going to put 'Henry' into
its own variable, which we happen to also call 'henry'.
So let's go ahead and run that and let's see what 'henry' is,
and let's see what 'my_cats'. Look at that.
So it actually took it out.
This is one of those times where you don't just want to use
this on itself,
otherwise it's not going to do anything.
It's just going to get rid of that last one.
Now, as an example, if we don't reassign this to a variable
or assign this to a variable, 'my_cats' is going to be '['Zephyr', 'Ezra']'.
But if I run 'pop' on this, again, it's going to take this last
item, and it's going to try to reassign it to a variable,
but there's no variable this time.
So all it's going to do is get rid of this last item, which
is the exact same as saying 'remove("Ezra")'.
But we don't want to do that this time.
We just want to 'pop' it.
Just get rid of that last one.
You can see it returns something called 'Ezra'.
But 'my_cats' is now just 'Zephyr'.
And this little piece of data was lost forever.
It was not stored in a variable of any kind,
it wasn't put anywhere else,
it just simply returned the item called 'Ezra'.
And Python said, "Okay. Well, you obviously don't want it.
Get rid of it", and updated our list with just 'Zephyr'.
Now, what I would like you to do for your fun little task
here, is make a list.
Your cats, your friends, your favorite hobbies, whatever it is,
and then I want you to, where is it append to it.
So add an item, and then I want you to remove one of those
items, and then I would also like you to 'pop' that item, or
'pop' the last item into its own variable.
Print all of that stuff out into your Jupyter Notebook or your
Python interactive shell.
And once you think you sort of have that figured out, let's
move on to that next lesson.