Hello guys welcome to the Python programming for beginners and in this post I'm going to show you what are lists and how to use lists in Python. So as the name suggests list is a collection of values, and how you can declare list in Python? you just need to give the name of the list for example, I want to have the list of names, so I can give the variable name for list
>>> names = []
>>>
as names is equal to and then the square bracket, and this square bracket will contain the collection of values and it's totally valid that this collection can you know initialize as zeros value that 0 values so the list can have 0 values also and it can have maximum values for example tens or hundreds of values so for example I initialize this list like this and this list is initialized as a blank list.
>>> names
[]
>>>
And when I call this names list once again it's a empty list because I haven't provided any value to the list, so it's totally valid that you initialize your list empty.
Now to initialize lists with some values what you can do is?, you can just write name of the list once again
>>> names = ["rico", "anne", "tyson"]
and then you can give the values inside your list like this, so for example I want to have the collection of some names
>>> names
['rico', 'anne', 'tyson']
>>>
And when I press enter your name variable which is a list is initialized with these values, now when you call this names variable once again it has these three values.
Now there is a term called index in Python for lists which indicates the position of the values in your list. So the position of this first name in the list in as any other good language starts from 0 it doesn't start from 1 but it starts from 0, and this is called the index of your list, so the index of this list or the index of this name rico is 0 index of this name anne is 1 and index of this name tyson is 2.
And on the basis of index you can even call these values, so for example I want to print out this first value called rico
>>> names[0]
'rico'
>>>
I can just try the name of my list names and then just give the index so the index 0 is 1 rico right, and when I press ENTER it returns me the value is rico.
Now there is a very interesting thing in Python is that you can even have index backwards also. So the forward index starts from 0 1 2 and the backward index starts from – 1, - 2, - 3. Because why this starts from minus 1 and this forward index starts from my zero, because you cannot have minus zero from backwards.
And you can even call these values on the basis of backward index also. For example I want to print this name tyson on the basis of backward index
>>> names[-1]
'tyson'
>>>
I can just do it like this, I can just give the backward index which is minus one which starts from minus 1 right backward index start from minus 1 and this prints tyson once again.
So always remember if you want to start from or you want to have the forward index just start from 0 1 2 and if you want to you know have the values from the backward index you can start from minus 1 minus 2 minus 3 and so on.
>>> names[-3]
'rico'
>>>
For example you want to call name rico you just need to give names and the backward index minus 3 that will give rico.
Now in order to add values more values to your list, what you can do is?, you can just call the variable or your list name and then you just write dot and just write append and in this bracket you just pass the value which you want to add to your list
>>> names.append("lucas")
so I want to add one more value to my name list
>>> names
['rico', 'anne', 'tyson', 'lucas']
>>>
and I press ENTER now this name which is a name of the list dot append which is a function built-in function in python has appended or added one more values to my names list. And now when I print my names list it has four values and it has added this extra name to my list.
Now for example you want to add lists into a list, so for example I have some other list
>>> ages = [23, 12, 32, 11]
called age for example age here. I have a list of names and I have the list of age right and I assign this list. And now I want to add the age to my a names list, how can I add?,
>>> names.extend(ages)
I can just write names which is the name of my name list dot extend and in the bracket you pass your second list so you can just pass this and press ENTER and it's totally valid
>>> names
['rico', 'anne', 'tyson', 'lucas', 23, 12, 32, 11]
>>>
and now when you call this name list you have extra list of age here. So there is an interesting fact about Python list is it can contain different data types so it's not necessary that you your list only if you initialize list by string values it only has to contain you know string values.
Your list can contain string values also or the integer values or float values or any other data type values and it's totally fine in Python. So you can define depth you know different data type in list also, so just remember this.
I will show you how you can remove items from a list.
So to remove our item from a list what you need to do is?
>>> names.remove("lucas")
For example my list name is names I want to remove some items, so I will just write names dot remove and in the bracket I will just give the item value, for example I want to remove this name lucas from the list. I will just say names which is the name of my list dot remove and then the value which you want to remove and press Enter.
>>> names
['rico', 'anne', 'tyson', 23, 12, 32, 11]
>>>
and once again I will call names and now the value lucas is gone from the list. In this way you can remove values from your list in Python.
Now for example if you want to print a list you can just call print method.
>>> print(names)
['rico', 'anne', 'tyson', 23, 12, 32, 11]
>>>
So you can just write names and press ENTER and we'll give print the list of name.
In order to print multiple lists for example I have the list called names and I have initialized this list called ages also,
>>> print(names, ages)
['rico', 'anne', 'tyson', 23, 12, 32, 11] [23, 12, 32, 11]
>>>
so I can just separate my list by comma and I will just add one more list and it will print two lists from me. First list is for names and second list of ages. Right, so you can either you use single list or multiple lists or print them.
Now in order to get you know I have shown you how you can get the maximum value minimum value or the length of the strings in the last tutorial right.
On the list also you can call length method len and you can find the length of the list like you just call len built-in function and just pass the list name as an argument.
>>> len(names)
7
>>>
I will give the length of the string and which is seven. So if the list contains seven values it will return seven, this is the length of the list.
Okay now you can even print for example
>>> max(names)
Traceback (most recent call last):
File "<pyshell#11>", line 1, in module
max(names)
TypeError: unorderable types: int() > str()
>>>
max if it applies to your list you can even call this method, it will give an error because my list is a mixed list.
>>> max(ages)
32
>>>
But the same method Max and I applied to the list called age it will give me the answer 32, because my list of age only contains the integer values and the maximum values of this is 32.
Okay so you can also apply built-in function on lists also. So I think in this way you can use list in Python, I hope your enjoyed this post.