Everybody and welcome to the next video.
So in previous videos I've talked about a list a few different occasions.
Now we're going to be going over what a list is.
So a list is nothing simple but what the list is what you would think of in real life.
So you have a order of different things combined with one name.
So you have a Christmas list or you have a shopping list.
So you have shopping list is the name.
So think of that as a variable.
And then the items that you need to pick up is part is combines the list so a way to look at this in
Python is so we can do temp list equals.
And then there's going to be block quotes so that we can do Brandon comma let's say twenty six for the
age and then where do I work.
Red team nation.
So you can see here the data doesn't have a key a key associated to it.
So it's just data inside of a list just a comma separated list of data which is perfectly fine.
You're going to find yourself using this quite a bit to hold commonality of data.
So for example this temp list might be user definition which might be a better option be using a dictionary
but for the sake of this particular tutorial we're going to be going over just using the symbol data.
So how will list works.
So a list we can go through and access slightly the same way that we do a dictionary.
However there's a different component here.
There's no key so we can't reference it based on name.
So we have to reference it versus index.
So what an index is is every element in an array.
So this is element.
This is an element and this is an element every element in a list comes back with a index number.
So the first one is zero.
Remember how I mentioned that we count machines count with zero first.
This is where it comes into play.
So zero would would be referencing to the string brand and one reference is the number twenty six and
then two references Red Team nation kind of show this.
What we can do is to print it out you do it similar the same exact way.
So temp list and block quotes and then you put the element.
So if I put zero here we should get Brandon.
Perfect.
If we put one we should get twenty six.
Perfect and then two we should get red team nation perfect.
So it's important understand that when you're accessing a list in most cases you are going to be accessing
it via either looping through and checking the value manually or you're going to reference it based
on a particular element of the list.
So if you generate a list and you know that the first element is an I.D..
So this guy is supposed to be an I.D. then you can automatically calculate Element Zero as your I.D.
and assign it accordingly.
So when you can assign it's a variable so if I did name as equal to temp list zero and then I print
NAME WE SHOULD GET BRENNAN perfect.
So you can assign it to anything you want.
Now what happens if we want to let's say overwrite what we have.
Right.
So let's say we want to change my age and say is my birthday and I wanted to up to twenty seven instead
of twenty six.
So what you can do is you can do temp list whatever element it is.
So in this case it's the first Ella Ertz element number 1 so 0 1.
And then we set it to twenty seven and that's it.
So we'll do the same trick we did with the dictionary as we'll just print out the list itself we run
this so we can see now the age shows twenty seven set of twenty six.
So that's how you can modify different elements in the list.
However to add and remove from a list is a slightly different concept so off list has something called
a function or a series of functions attached to them.
So like how we do with the dictionary with the DOT values we can do.
We can do append which appends the new value to the end of the list and then dot remove which removes
a specific value from the list.
So if we do you temp list append and we do let's do title will do CEO right.
Then we print temp list.
Now if we run this we now see that this was our list when we changed it to twenty seven and then we
appended CEO to the list.
Now we have a CEO at the end.
What if we want to remove the list remove CEO let's say let's say I got fired.
So temp list dot remove and then whatever the value is.
So in this case CEO.
Now you can do it based on index.
However in this case we're going to use the value itself so we can see open never printed it back out
again.
Copy that printed out again.
Perfect.
Now we can see we added it here and then we removed it here.
Awesome.
Now how do we loop through a list.
So there's a few ways you can do it.
You can do it similar to.
We can do it the python way which is looping through the list as is or we can loop through it based
on an index similar to what we did with a dictionary.
So the first thing we want to do is we're going to look through this in a more Python way.
So this is the more common way you're going to look through something or look through lists of four
elements or just that can be named X. So we can do X here in temp list that's it can print x.
Now what's common here is that your list isn't going to be called a temp list.
Most likely this is going to be called something specific such as ports or whatever your application
is holding.
So whatever that list is holding you're going to have it called here.
So you can say so if this was said Port list I would say for port in Port list.
Hopefully that makes sense.
It's just a better naming convention for when you are writing code.
So people following you or having to look at it later they can understand what that list had.
You don't want to name things temp this temp that you want to have the name of what the the actual value
is.
So what does this reference.
What is this what should be in this particular variable what should be in this dictionary or a list.
Now there's another way you can do this is most people will put a comment above a some code explaining
what's in a dictionary.
So what are some of the basic stuff that you're going to find.
So we haven't gone over comments yet.
So what a comment is is the hash sign.
So because of that or the pound sign this is ignored by the interpreter.
So you can put testing here not running it doesn't matter if you put code here.
So if we do print in comment that will never that will never run.
So just as an example here when we run this you'll see that there's nothing up here that says in comment.
So a comment there for you to write aspects of your own code.
So you can comment things and let people know what it's doing.
However if we look here we did our for x in templates no printing X we can see we printed Brandon twenty
seven.
Red team nation.
So we know that that's working.
So what if we want to print it based on index so let's say we want to modify text as we go right.
So we'll do a quick print here.
Do a new line to give us some some room here so for I in range.
Now this is going to be a new method that we want to do.
So Len so Len is short for the length.
So it gets the length.
So if you do Len of a string it'll give you the amount of characters etc. So when you do a Len of a
list it grabs the amount of elements.
So in our case we have 0 1 2.
So we've got two.
So what this is doing is it's passing the length or the actual digit of the amount of elements in the
range and then range.
I will then be starting at 0 and going all the way up to whatever the max amount is which is important
because if we do print temp lists and we throw an eye inside of the block quotes I is standing 4 0 1
2 3 etc. Now because of that it references 0 1 2.
So if we go ahead and run this again we'll see Brandon twenty seven termination.
However if we can we can modify this in line so if we do if I is equal to let's say 0 1 so 1.
So if it's our age we want to set temp list I equal to let's say are equal to 28 let's say at another
birthday.
So we can see here that when we ran it the first time we got twenty seven we ran it again we got twenty
eight.
And what's important here is that it persists.
So if we print after all this is done we print out the final list as is we can see that now it's set
to 28.
So play around with lists get familiar with them you will use them a lot you'll find when you start
loading in data you're just gonna throw things in the lists just to make things a little bit more uniform
and easy to work with.