Transcript for:
Python Programming Lecture: Lists

so in the series of learning Python programming language previous video was a coding exercise we have designed a log calculator in which we have used if nestedly fails a leave statements because these control statements we have discussed right what is simple if nested if if else necessary fails L if and all we have discussed right so now in this video we'll be talking about A New Concept in Python that is python list right what is list why we use list some features characteristics of list and as well as some functions or you can say methods of list that can be applied on list how to create a list and all we'll be discussing in this video with the help of program practical I'll show you right see first thing ah let me just ask one question if I want to store like one integer suppose I want to store 10 so simply what we do just create a variable like a and is equal to 10 so the stand would be stored here some a is a variable in right simply one name Jenny I want to store one another variable I can take like name name is equal to gen simple this is string this is of integer right same Boolean type we can store same flow type we can store like I am going to store B is equal to 10.10 so this is float but the problem is if I want to store suppose I in my class I have a 15 students so I want to store roll number of 15 students so one method is what the method still now we have discussed is what we can create 10 variables A B C D and in individual we can store one one roll number right but that is I guess not a good eye idea right if suppose I want to store in my class I have 60 students so I'm going to store roll numbers of 60 students and names of 60 students then you have to create 60 variables right because in one variable we can store only one one value right but I guess that is not a good idea to create 60 variables to store 60 roll number of 60 students that would be very time consuming length you know code would be very lengthy and error prone code would be there that was not readable many problems will be there so is there any way under a single variable name we can store roll number of 10 student or 60 students or 100 students under a single variable name yes we can do with the help of list right there are other ways also in Python Tuple set dictionary those also will be discussing one by one in later video but here you will be discussing list right so what we can do I can create some simple a variable suppose I am taking row number right and then I can store these square brackets opening brackets roll numbers of 5 10 or 50 or 60 students as many students as you want like I'm taking only five students one two three four five and closing square bracket that's it so this is what I list right under a single variable name we have stored these roll number of five students so basically this list is what it's a data structure it's just a way of storing and organizing the data it comes under sequence data type because it contains sequence of you know types of data and see if once list I am creating for names names is equal to like this so this is these are strings these are integers right but we can have mixed type of list suppose in one list I am taking one integer and one is string one is true or false these are Boolean data type 10.10 this is float so uh suppose this I am taking mix underscore list so this is also allowed collection of different different data types also allowed same data type also allowed like uh if you have read the array Concept in C then array is what collection of similar data type homogeneous right so if this integer integer data you want to store then in one array you can store only integer types of data only it's not like that this you this is allowed no but unless it is allowed a mixed list is also possible integer string Boolean float this is valid list this is also valid this is also valid so this is I think very powerful tool of Python language the list and still there are many functions and many you know interesting things we can do with this those also will be discussing so basically list is what you can say it's a container or it's you can say collection of things enclosed in square brackets and separated by comma that is a list it is you can say it's a dynamically sized array not fixed size array dynamically sized array we can say it's just a container or you can say collection of things and list is what are sequence air type because it is this is just a sequence of data it is used to store sequence of various data types and it is not homogeneous need not to be homogeneous right a single list can contain many data types like integer string Boolean float and as well as objects right now some characteristics are of list you can say like but these are ordered list are ordered these items are having a defined order and these orders this order of these items will not change right one more characteristic is what these are new table two words are there mutable immutable simply mutable means in simple words I'm taking can change immutable means cannot change mutable means after creating the list we can alter the list we can change the list we can add some element we can remove some element we can you know change the hair suppose here I have Ram we can change this name I can do this Mohan so this alteration is possible after creating the list so let's start mutable but list are ordered as well as we can access how you can print the list now if I I want to print the store number so simply print and name of the list so it will print this thing output would be something like this one two three four five in these square brackets right but if you want to access individual elements suppose I want to access this three that is also possible using Index right now index is what index of this is index will be started from 0 0 1 2 3 4. why this is from 0 it is not you can say position it is you can say offset from the starting position so so from the starting position this is starting position you can say the shifting or offset of the variable so offset of this variable from starting is what nothing it's 0 or shifting is zero so that is why index is 0. shifting of this is from starting position is one then two then three then four that is why index generally starts in programming from zero so for programmers counting starts from zero actually right so using index we can access like uh if I write print roll number and in these brackets just put the index suppose I am putting one so at 1 at this index what is 2 so it will print 2 so you can access individual elements also right and many operation we can perform on list like we can sort the list we can reverse the list we can append more data we can extend the list right we can count the no occurrence of these items in the list we can find out index of individual item in the list you can find out length of the list minimum or maximum from the list many methods are there so those methods will be discussing with the help of a program I'll show you practically right but you don't have to remember all the methods just take a look like because everything is available on this documentation on the Google can you see the python documentation many methods are available so don't have no you don't have to memorize all the methods right just take a look how to yes we can do this thing with list we can do this thing with list right so that you have in your mind yes you can do you can sort the list there is a method you don't have to write that thing right you just Google that method how to you know use that method and that's it and yes one more thing in list list can have duplicates value like five and comma one more item I want to add 2 comma one so these are duplicates so this is allowed in set actually actually duplicates are not allowed right but unless you duplicates are allowed right so these are some character the characteristics also uh you know of list so let's see some functions of list so let's create a new file python list Dot py right and we will be taking first of all our list suppose numbers so suppose I have like only four elements 10 0 minus one seven and if you want to print this you can simply print that's it so let me just run this and it will print C this thing with brackets right rather than this if I want to store some names that is also fine you can store like this if you want to store names that is also fine this is also fine if you want to print simply print and names so it will print this list also right as you can see mixed list is also possible like uh if I take a mix underscore list or name and yeah integer then I am going to store like Jenny then I moved to store like true Boolean data type and 10.10 something like this and if I print this mix list then it will print okay rather than this we have an error like this p r i n g it's print node p r i n g p r i n t right let's run this so see it is printing this so mix is also possible but now let's comment on these lists and if you want to access individual using index individual you know these items in the list then you can access like number one so now it will print what 0 because at one index 0 is there rather than one if you will if you will write six obviously six is not in this list the index six sixth right so it will give error like list index out of range so you can up to 0 1 2 3 up to three index you can put if you put three then it will print seven right if you want to print one function is like length then you can simply use one function length and in bracket you can simply put the name of the list numbers so see length would be four one two three four right so this is what length but if you want to access like this is something called as slicing but negative indexing is also possible see let me tell you print numbers if I write minus 1 minus 1 negative index start from backward so minus one index is for this element seven so it will print seven see if I write minus 2 then it will print minus one so minus 1 minus 2 minus 3 minus 4 something like this so backward negative indexing is also available in in list now what is list slicing see if I write something like this starting index is 0 colon what is length of the list four so if I write 0 colon 4 it will print simple the complete list if I do not write this 4 just blank 0 nothing so by default it will take 4 here it will print the list if I do not write a 0 simply the colon so by default starting is starting point is starting index ending point is length by default these numbers are there so still it will print to the list but if I do string slicing if I write index should be started from 1. and 1 2 suppose 3. so one is the index so 0 and 3 is what till third length so one two three mean till minus one so it will print 0 and minus 1 only see 0 and minus 1. right if list is something like this 7 8 10 comma suppose minus 67. and now if I print one two four so see now what it will print index 1 0 from 0 right because at one index 0 is there 0 to this 4 is not index because index so of 0 1 2 3 4 index of 8 is 4 so means you think that it will print till eight no this 4 is what length till 4 length this is length so length would be started from 1 1 2 3 4. so till 7 we have the four length fourth length of the string means length length four of the string so it will print till 7 only so 0 minus one seven right so you can say whatever you will provide here four that minus one index it will print right so means till third index it will print zero one two three third index 7 is having index three right so this is what string slicing if I do not write anything here so from starting from 0 it will print by default last argument is what complete length of the stream right and if I do not put somewhere here and if I write fifth till fifth length it will print so from starting it will print in eight till fifth length and by default it is zero right but one more is what if you want to print the sub list or you can say this is about extended slicing like from 0 to length five one more colon and third argument is the steps suppose I am writing here 1. right so one step it will Skip and it will print the next like one step is nothing it will skip so okay let me show you first of all from 0 to fifth length so ten zero minus one seven eight okay but rather than one if I write 2 here then it will jump Two Step C at starting from zeroth index so 10 then steps are two so like from 10 10 to 0 1 then 2 so 0 it will Skip and it will print next element minus one then one then two then after second step it is eight so eight it will print till fifth index it should print right so this is what extended slicing if I write here 3. then what it will print 10 okay sorry see it will print only 10 and 7 because it would be started from 10 from 0th index 10 then it will skip 2 because third step so one two three at third step we have seven so seven it will print one two three minus 67 but till fifth length we have to print if you do not write five here then by default it will print minus 67 also because till the end it will print right so I hope you got and now some functions like if you want to sort the list so we can simply write what like numbers the list name dot a function we have salt and then we can simply print the list numbers right let's see what happens that would be in sorted order right so duplicates are allowed 10 10 we have I have W duplicates are allowed in list right but if you think directly here I can print like in one line only rather than writing it in two line numbers print so it will print nothing none it will give because this function will return nothing no return type right so we cannot do this thing you have to sort first right and then you have to print the list because it will change this original list only right now one function is reverse so rather than sort we can do reverse right and if I print then it would be reverse of the list minus 67 10 8 7 minus 1 0 10. so this reverse will do reverse of the list as the name suggests a length we can find out maximum minimum just you can write down Min and Max so this main function will simply print the minimum from the list if I write like Max then Maxwell return simply the max Max is 10 but one thing we cannot apply this sort on the mix list this will give error if I do something like this if I apply the sort on this mix list having the list is having mixed data type and if I run this then C this less than not supported between instance of string and end because this is int this is string this is Boolean so we cannot compare different different data types so sorting on mixed a mixed list is not allowed right now if you want to add something in the list so there are three methods basically so one is you can append you can use append append means at the end of the list we can add one element at a time one element at a time at the end of the list if you want to add then you can use a pen so how you can use that thing just the list name of the list numbers then dot then append and the value what you want to add 45 and after that I am printing this numbers so let's run this see now 45 is at the end of the list right one more append you can 46 47 like this these will be added at the end of the list but at one element at a time so generally what we do we create a you know empty list like numbers just the brackets nothing and then we append the numbers right in the list that also we can do so next is you can insert but at specific position I want to insert a data node at the end specific index like at second index third index then we can use insert function so here what we can do like numbers rather than append we can do insert but in append we pass only one argument the value you want to add but here you will pass two argument first argument is at which index you want to add suppose that uh Second index I want to add right comma what you want to add 45 or I want to add at second means 0 1 2 means here at second index 45 would be added and this would be shifted to right side all the data let me just run this and that you then you will get it below see 10 0 then 45 right at second index then minus one seven eight it's not like that we will replace this minus 1 because that second index minus 1 is there maybe you will think it would be replaced with 45 no it will add 45. minus 1 would be still in the list and would be right shifted so this is our particular position you can insert one is if I want to add more than one data at one at same time right then using a pen we can only add one data using insert also we can at one time we can add only one data yes you can use insert in third and set to do three times if you want to add two to three items but at the same time you want to add three items then one function is extend so that you can do extend and in bracket now we can pass list like I want to add 45 46 47 8 9 this list I want to add and simply we just print the numbers now see this list now extended at the end of course minus 67 45 46 47 for 78 89 this list would be added more than one item but at the end of the list extend also add at the end of the list more than one item append will add at the end of end of the list at only one item at a single time at one time right so this is what extended list now right one more thing what we can do like if you want to change like At first index I have 0 but I want to change the zero so what we can do like numbers the name of the list then at which index you want to change the data first index what you want to insert now rather than 0 I want to insert suppose 45 here and now I'll print numbers so this at the place of zero now I have 45 in the list right so this is how you can change the list so that is why I told you list our new table we can alter the list after creation of the list we can add change the data and remove and remove the data now I want to remove something okay now one more thing if I want if I do something like this index 1 2 4. one two four means one two three three element I want to add so if I do something like this 45 46 47. now what will happen C these three elements from the index is 1 and this 4 is what not in the X this is length till fourth length means till Index this length minus 1 till third index so it is first index second index third index 45 46 47 rather than 0 minus one and seven and all the other elements would be same right so you can modify more than one item also you just have to provide the range where you want to modify right now if you want to remove something then we have two methods remove as well as Pop remove will search the item and just remove it will not return anything so like sorry first the name of the list numbers dot remove and in bracket we will pass the value the item you want to remove right so suppose I want to remove 0 from this this list so let's run this C 0 is not there now see maybe you are thinking we have modified this list here like extend and all but I have just commented out these line so original list is now this right and on this all only I am performing this remove operation so it will not return anything it will just search the item and remove but it will remove the first occurrence of that letter suppose here I have one more zero two zero are there and now I am removing zero let's run this but still in the list we have 0 but first occurrence it will search from starting it will find 0 here it will remove 0 and it will print the list it will not remove this zero removal remove only first occurrence of that letter right now pop number stored pop yeah it will return what pop means it will chop off the remove the last element by default the last element of the list so if now I print numbers then see now then there is the last zero has been removed so Pope will remove the last zero and if it will remove that it will remove as well as it will return that element so if I want to print what this Pope has removed from the list so it will print that element also it will return that element C 0. 0 has been removed and after that we have printed the list right but if you want to remove specific element then you just pass the index in this box suppose I I want to remove not the last zero this is zero so index of this zero is one now let me just run this see now it has removed zero but but from first index last zero is as it is right so just pass the index if I pass 5 so it will delete this file this L this 10 at the fifth Index this 10 is there so it will remove the sand it will print the list so this is how pop will work and there are many methods see as you can see here this this uh the python documentation of python here there are many methods like appendix stand insert remove pop clear index count sort reverse copy and how to use these method with one example they have given so don't do not have to memorize these methods you can just search but you just have to go through these methods yeah we can can you reverse a string then you have in your mind yes you should know you can reverse the string and then how to reverse that you can simply search so you just try out these methods this count will return the number of times that item appears in the list so this you have to try out index if you find out the index of a particular item in the list then also you can find out right clear will remove all the element from the list so if you want to count like numbers don't count how many times 0 is there in the list so you can just run this and 0 is 2 times right so count will just return the number of times the item is present in the list so I hope you got what our list why List have been introduced what are the advantages benefits of list what are some methods of list at all you have got everything right so in the next video we'll be seeing what is random module and then we'll see you on coding exercise so now I'll show you the next video till number bye take care