Welcome back, everyone, to this lecture, we're going to be discussing lists in Python.
Now, a data structure in Python that can hold elements in a sequence defined by brackets where each
element is spaced out by a comma.
So here, for example, we can see a list and this list has five items in it.
The first one is a string just item.
Next one is an integer zero.
The third one is a floating point number ten point two.
Fourth one is another string, and then the fifth one is actually a variable.
We don't actually know what this variable is.
It's just my VAR, but we can imagine it's could be a string and could be a number or something else.
And typically, when we're organized things in a list as we get better and better, if Python, we'll
probably just be putting a variables in there to some degree.
Now we can use these actual lists to organize data in sequence ordering, meaning we can then grab individual
elements from that list using the same sort of index bracket notation, as well as index slice notation
that we learned earlier for strings.
Let's explore the basics of Python lists.
All right.
Here I am inside Visual Studio Code Editor.
If you want to create a list again, you just use square brackets and then commas to separate the items
in the list.
So, for example, I can say something like my list is equal to and then square brackets.
And you can start putting in items.
So this is a list containing three integers one two three.
If you had another variable there or something like, let's say my variable is equal to 10, I could
maybe add in or replace one of these with another variable like this.
So let's go ahead and print out my list just so I can see what it looks like.
So I'll say print my list and we should expect to see one two three 10.
So I'll save that change.
Let's go ahead and run Python example.
And there I see it.
One two three 10.
OK.
Now why would we want to use a list?
A list is really useful when you have a bunch of different types of variables, like either numbers
or strings or other variables that you want to make sure are organized in a specific order.
That way, you can actually later on iterate through that list, or you can grab particular elements
from that list just as we could with a string.
So just like a string is a sequence of characters.
A list is just a sequence of items.
It also starts to indexing at zero.
So to make it clear, I'm going to say my list and have it be 100, 200 and 300 as the items in the
list.
And let's actually go up to 500.
There we go.
So I'll save that change.
Imagine I want to grab the item 100.
How do I do that?
I simply say my list.
And then using the square bracket notation, just like we did four strings, I can pass on a zero,
which says, grab these zero index item from that list.
So I should see this print out the number 100 and there it is 100.
Because again, all I'm saying is grab that first item at index zero.
Now, let's imagine I actually want to take a slice.
How does that work?
Works the exact same way it did for strings.
You have the starting index, then you say Kolan up to the end index.
Imagine I wanted to grab 100, 200 and 300.
How would that work?
Well, I'm starting index zero and then I go up to, but not including index three.
So it would be 100 Index zero index one index two.
And then I wanted to go up to, but not including index three.
So in order to get 100, 200, 300 and you say go up to index three, save that change.
Run this and I seen 100, 200 300.
Is a little more practice.
Imagine I wanted to grab 200 and 300.
How would that work?
Well, to grab 200, I need to start an index one.
And then to grab up to 300, I need to go up to, but not including index.
This is index for or, excuse me, three zero one two three.
Yup.
The real say that and this should bring back 200 300.
And there it is.
If you ever confused on this, I would just encourage you to practice a lot of slicing.
Just make yourself a really long list.
Try to get a particular slice and see if you're indexing starts and stops makes sense.
Now how do we add a new item to a list that already exists?
For example, let's imagine I wanted to add in my variable into the end of this list.
So let's go ahead and make it really obvious.
I'm going to say new as a string, and I want to append it to the list.
How does that work?
This is where I get to call a method.
So I'm going to say my list and I call the append method.
It's one of the most common methods you call on a list.
And this allows you to append something at the end of a list, and we'll see how to insert something
later on anywhere in a list.
But I can say my list append.
Go ahead and append that variable, and then I'm going to print out.
My list.
And let me put this all kind of line after line here.
So again, all I'm doing is I'm saying, go ahead and take that variable and append it to the end of
the list.
Save that change.
And when you print out this list, you should now see it as one hundred or three and four to five hundred
and then the item new has been appended to the list.
OK, now what if you wanted to insert some item somewhere inside the list that a particular index,
a pen will always go at the end?
If you want to insert something at a particular index location, you can use the insert method.
Open close parentheses and then where do you want to or what do you want to insert and where do you
want to insert it?
So it's actually going to take in two arguments.
You need to know where you want to insert it and what you want to insert.
So where just means an index location, which means this method actually takes in what is known as arguments.
You pass in the index location, you want to insert it.
For example, let's say I want to insert an index location zero, then I'm going to say, Comma, what
do I want to insert?
And I want to insert my variable here.
I remember each time I saved the script, I am essentially kind of overwriting everything I've done
in the past, so we shouldn't expect it to have new still at the end of that list.
Remember, I'm essentially redefining that list again from 100 to 500 here.
So let's save this and see how this works.
And now I see new at the beginning of that list.
Does that make sense?
Hopefully it does.
What I'm saying is go ahead and take my list insert at index location zero.
This new variable?
Let's go and experiment what happens if I insert it to index one?
Then I run this the way this works is you should expect the final output result to have this variable
at this index.
Index one, which is true 100 Index zero now and Mew is an index one.
So that's how you can add new items to a list append if you just want to add it to the end of the the
list.
That's really useful when you keep iterating and keep adding items and don't want to upend the order
that is already in your list.
And then you have insert when you maybe want to insert in a very specific position.
That kind of implies that you know the list really well and know a lot of specifics about the current
items in the list and where indices should go.
That's why you actually don't see insert use that often compared to Append, because again, Insert
implies, Hey, I know the list really well, and I know this very specific variable has to go to this
very specific index versus append.
You just say, OK, just keep adding it to the end of that list.
And now I have kind of almost like a history of what I added to the list as I keep adding, adding more
and more things.
Now you may be wondering, how do I actually remove an item from a list?
The way this works is with the pop method.
So I'm going to say item removed as a new variable and then I'm going to set it equal to and the way
you remove an item from a list as you pop it out, you can say my list dot pop.
And then if you just say dot pop by itself, it's going to remove the very last item in the list.
So what I'm going to do is, in fact, we don't actually need my variable anymore, since we won't be
using it.
I'm going to say my list is equal to these numbers.
Then item removed is equal to my list dot pop.
Notice that pop is actually going to return in item and then I'm going to print out my list and then
I'm also going to print out the item that was removed.
So I save those changes.
I run this, and now I see the following.
So I get 100, 200, 300, 400 and then I see 500.
So by calling Pop, it removed the last item in this list and then it actually gave it to be assigned
to this variable.
Now, do you actually need to assign this to a variable like item removed?
No, you do not.
If you really wanted to, you could just say my list that pop.
And now notice Visual Studio Code is warning us, Hey, by the way, this isn't defined anywhere.
So that's OK.
If I going to comment this out to put a comment in Python, it's just this hash tag symbol so you can
put a hashtag in front of that.
That's only a comment.
Go ahead and save those changes.
And what's going to happen is you will still see the actual pop occur.
So five hundred is no longer in that list.
You just decided not to save it to any particular variable name.
Depending on the situation, you may just be popping things off a list.
Now, let's imagine you want to actually remove at a specific index like index zero.
You just pass in the index, you want to remove it.
So if I say pop zero, that says go to index zero this list and just remove that item.
So now when I run this, I should see 200 through 500, and there I see it 200 through 500.
The last thing I want to point out is you do have the ability to reverse the order of a list by calling
the reverse method, so you can say my list.
Reverse open, close parentheses.
And now look what's going to happen when I prints out my list.
It is now in reverse order 500, 400, 300 200, et cetera.
There is also the ability to sort a list depending on what items you have in your list.
It may make sense to sort them now if you have a list of a bunch of different mixed data types like
numbers and characters and strings and letters.
It may not make too much sense to sort them, but if you have a bunch of numbers, then it could make
sense to order them or sort them in ascending or descending order.
It could also make sense to organize things in alphabetical order.
That's pretty much the two use cases.
You see this when you call that sort open close parentheses.
If you're super advanced, well, maybe not super advanced, but if you're getting more advanced than
Python, eventually you realize you can actually sort by a particular key, which means you can have
a custom definition of how you want to sort things.
We're just going to ignore that for right now.
And instead, I'm going to put in a bunch of random numbers here.
So go ahead and just kind of input.
A bunch of random numbers doesn't matter.
They don't need to be in order.
And then what we're going to do here is call my list that sort.
And then print my list and let's see the effects.
And there you have it now, it's sorted in ascending order, starting from the east all the way to the
greatest.
Now the very last thing I want to point out before and this lecture, that's sometimes a common question
is the default way of sorting is ascending.
But what if I want things in descending order?
How can I actually do that?
Well, there are many different ways of doing that, but one way to combine two ideas that we already
thought about is to actually call one method right after the other.
I could first sort my list and then I could come here and call my list and then call reverse on it.
So then I'm sorting in ascending order and then simply putting it in reverse.
So I'm going from random number order, sorting it out and then reversing it, and let's go ahead and
see the results there.
And there you have it.
Fifty nine and then onwards down to one.
OK.
That's it for the basics of lists.
I'll see you at the next lecture.