Hey guys welcome to the next blog on python tutorial for beginners in this blog I'm going to show you how to use lists in Python so first of all what are lists so list in Python is a kind of collection which allows us to put many values in a single variable you can also say that list is an ordered set of values so let's define a list so I'm going to just say X is equal to and you can define a list inside these square brackets and inside the list you provide some values so for example I'm going to provide the values 3 comma 5 comma 4 comma 9 comma 7 comma 10 so this is how you can create a list in Python and when I press ENTER it's going to create a list and when I just use X to get the list is going to provide me all the values in the list now these values which are inside the list are called elements so 3 is an element or 5 is an element or all these values in this list are all called elements and all these elements are ordered by their index now index as I already said in the previous blogs also always starts from 0 so we can fetch the value which is at index 0 using the square bracket once again with a variable name and then when I write index 0 it's going to give me the element which is saved at index 0 so looking at this list 3 is at index 0 5 is at index 1 4 is at index 2 and so on so let's say I want to get the value which is at index 4 0 1 2 3 4 so I'm going to just write X square bracket 4 and it is going to give me 7 which is saved at index 4 now it's not necessary that list must contain all the same data-type elements so for example I can also declare a variable called Y and in this I can save some name for example max I can save integer let's say 1 I can save a decimal value let's say 15 point 5 I can save other lists inside this list using these square brackets for example 3 comma 2 so one list can contain different type of data types with any number of elements so when I press ENTER and this time I want to get the value which is at index 0 it's going to give me max and once I want to get the value which is saved at index 3 then it's going to give me this list which I have saved at index 3 now when you try to access the index which is not there so I'm going to just write Y and then I'm going to say hundred and this value is not present in this list at this index so I'm going to press ENTER and it's going to give me the error which says list index out of range now if you want to get the length of the list you can use the inbuilt function in Python which is le n and then your list name so for example I want to find out the length of list X it's going to give me 6 it's going to give me 6 because you can see here this list contains 6 elements or I want to get the length of the second list which is why it's going to give us 4 because this list contain 4 elements now it's also possible to insert and remove elements from the list so for example I'm going to use X which has 6 element till now and I'm going to insert the 7th element so I can use a method called insert and you can see this method takes an index and the object name so I'm going to choose this and the index at which I want to insert the value is at index 2 and the value I want to insert is some string for example Tom and I'm going to press Enter I'm going to print the values inside the X list then you can see now this list has seven elements and Tom is inserted at the index to because we have inserted this value at the index two in a similar way you can remove something from a list also so to remove something you can use your variable name for example X dot remove and the value you want to remove so for example once again I want to remove this name which I have inserted at index two and once again when I print the value of X it's going to give me these values now let's say the list contains two element which are exactly the same so I'm going to insert one more element at index one and this element will be three for example and now my list contain double three here and if I want to remove this three so I'm going to just call this remove function once again and I will say three here and then press Enter you can see only one three is removed from this list so you can see these three are at index zero and index one so this value is removed which is at index zero and this will remain in this list so if a list contains duplicate values is going to remove the value from the left and also remove is going to remove only one object which is found first in the list now if you try to remove the element which is not there in the list so I'm going to remove something which is not even there in this list then it's going to give us this error it says that this is a value error and it cannot remove this value from the list now there is one more function with the list which is a pop method and this we can use to remove the values from the last so you can see our list was previously having six elements and ten was the last element and when you use this function which is pop it's going to pop this ten from the list and now let's see the content of the list so now this list contain only if five element and the last element is now removed once again when you use this pop method it's going to remove the seven and now our list only contains four elements now let's say you want to delete the whole list so I'm going to declare a list and this list will contain a few values and then I will press enter and you can see the level use of list and now I can use a function called de L and then the name of the list and this function is going to delete this list so I'm going to just press ENTER and then once again try to access the values inside this variable and now it says the name Z is not defined because delete function has deleted this list now once again I'm going to create this list Z and now there is a function called clear which is used to remove all the values from the list so I'm going to just press ENTER and then try to access the value of Z and you can see this list is now empty because we have used the clear function which is used to empty the list let me show you a few more functions related to lists so let's say I can use X and the values inside X are 3 5 4 9 and I can use a function called sort to sort all these integer values inside the list and once I press enter now now you can see all the values are sorted and you can see all the values are sorted in ascending order you can also use this method X dot reverse and it's going to reverse all the values so once I use the reverse function and then when I try to get all the values inside the list you can see all the values are now reversed there is also a function called append and here you can append anything to your list so I'm going to append 10 to my list and when I try to access this list you can see at last this 10 is appended in my list also if you want to copy one list into another you can use let's say I'm going to declare a variable called s here and want to copy all the values from X list I can use X dot copy and this is going to copy all the content of my X list into this new variable called s so you can see all the values are copied into the variable s so let me append something into my list once again so I'm going to append tenmonths again and now my list looks like this and there is a function called count using which you can count the number of element which are there in the list so I want to find how many number of 10 are there in the list is going to return me to and once again I want to count how many threes are there in the list it's going to return me 1 let's say I want to count how many hundreds are there in my list and it's going to return zero so this is how you can use lists in Python you can also use the list name and then dot and see all the methods which are available related to list here and you can use these method which you see here so I hope you have enjoyed this blog and I will see you in the next blog
Post a Comment