Transcript for:
List Data Structure Lecture Summary

hi guys and welcome to my channel in this video I'm going to teach you about list data structure together we are going to create an application so that you can practice and apply everything that you learn so let's go straight to the point a list in any programming language is a data structure so it is used to store a collection of data which means multiple items of the same type and the reason why you would put your data in a list is because it makes it easier and faster to work with your data so it is easy to add new elements to the list to remove existing elements to order your data sorted by some criteria and so on so let me give you a few examples of lists in real life and in programming so in real life it is easy to create let's say a shopping list so you would simply write down all of the items that you want to buy so when you go shopping and you buy those items you simply check them or you scratch them off and you remove them from your list and the same applies in programming so you can easily create a list programmatically and to that list you can easily add new elements or remove existing elements now something that is a benefit in programming but it is not as easy in real life is that in programming lists give you a lot of options to work with your data in an easy and quick way for example if you have a list of all of the students of some University in real life it would take you a lot of time in order to let's say sort those students alphabetically or sort them by grade or any other criteria but in programming that is something that you can do in a matter of seconds so in programming we use lists in order to store our data and then to work with that data in a quick and easy way a list can contain multiple elements but it can also be an empty list which means that it contains zero elements an example of a list like that would be a list of all of the boys that I've dated Now counter of that list would be zero but that is still perfectly valid list the entire purpose of using lists is to organize your data so that it is easy and fast to work with that data for example you can make a program where you can create a list of all of the boys or girls that you like and then you can write a message that says hi you're very pretty would you like to go on a date and then instead of of sending that message to each person individually what you can do is you can send it to the entire list so this way using lists is going to save you a lot of time but this is not a program that we will build today because I'm not teaching you how to be a playboy I'm teaching you how to be a programmer which is basically the same thing and before we start building our application there is something important that I want to tell you so if you enjoy my teaching style and if you like learning from my YouTube videos then you can definitely learn a lot more from my practical programming course you can find it at this site here code beuty academy.com this course will cover everything that you need from beginner to expert and I will be there for all of your questions to speed up your progress and to help you start a successful career in just a few months a lot of students who joined code be Academy just four months ago have already started their first job thanks to the things that they learned in this course so why wouldn't you also be one of them and if you're not sure if this course is for you or not you can try it out for 7 days because we have 7-Day money back guarantee which means that if you don't like it for any reason you will get all of your money back no questions asked so now instead of coming up with excuses use the discount code that I will leave in the description to finally learn programming and enjoy all of the benefits that programming career offers now let's return to our program and let me show you how to create a list so you simply say list and then inside these angled brackets you put the type of data that you will store in your list so I want to have a list of strings so I will say please create a list of strings and then I need to give a name to my list so let's call it shopping list like this so here I have created a list of strings it's it is called shopping list and now what I want to do is I want to initialize that list to be empty at the moment so you do that by simply saying please create a new list of strings and you use these parentheses to indicate that right now it is going to be an empty list so this is how you create an empty list but you can also create a list that contains some elements so instead of using these closed parentheses that are te you will use curly brackets instead and then inside these curly brackets you will put the elements of your list so since we have specified that this is going to be a list of string that means that all of the elements of this list should be strings so in order to specify the elements you use these double quotation marks because we are talking about strings so the first element of my shopping list will be for example chocolate like this and then if I want to specify the second element I need to add this comma sign okay so here I will add the second element let's say banana okay and then let's say sugar and let's add also milk and Fifth Element will be for example cereal okay so this list contains five elements and again since the elements of this list should be string you need to specify them under these quotation marks and also you need to separate elements with this comma sign and also please notice that the last element doesn't have this comma sign because after it we don't have any more element so we don't need comma sign so this is how you can create a list that initially contains some elements so when this list here is added I have said that it will contain these five elements now the question is how can we print all of these elements of the list to our user how can we show them in our console it is actually very easy to do that we have a special Loop that is called for each and this is how you write it so you say for each and then inside these parentheses you say for each string item in shopping list and then here you specify what do you want to happen for for each item in your shopping list and here I simply want to write each item in the console so once more for each Loop is used in order to iterate through all of the items of your list so here you say for each item and then this is the type of your item so item is of type string so for each item in my shopping list what I want to do is I simply want to print that it item in the console so now if I start the program as you can see here is our entire list printed in the console these items are the ones that we have added to our list initially which means when we created this list it was created with these initial items so now the question is is there a way to add new items to the list after we have created it and the answer is yes and it is actually very simple to do that so let's do it here before our for each Loop you simply say shopping list. add and then inside these parentheses you specify the item that you want to add so I will put it under these quotation marks because we are adding a string so what do I want to add to my shopping list well let's say lemon okay and then let's add one more item so let's also add Apple like this so now this list should contain these initial five items plus these two items that we have added after the list was created so now if I start the program as you can see now our list contains both these five initial items and then these two newly added items so as I just demonstrated it is super easy to add new items to your list you simply say shopping list which is the name of your list and then dot add and then you specify the item that you want to add to your list so let me show you another interesting and important functionality and that is the following if you take a look at these items of the list you will notice that they are printed in the exact same order that we added them in so here we first added chocolate and then banana and then sugar milk cereal lemon and apple and we have that same order here so now the question is is there an easy way to sort these items let's say that we want to sort them alphabetically so is there an easy way to do that and the answer is yes actually there is an extremely easy way to sort your items so here after I have added all of my items I can simply say shopping list. sort and this method here is going to sort all of the elements of my shopping list so now when I print them they will be in the alphabetical order so if I start a program as you can see we first have apple and then banana and then cereal chocolate lemon milk and sugar and since the elements of our list are strings that means that sort method is going to sort them in the alphabetical order so from A to Z but if the elements of your list are for example numbers then sort method is going to sort them from from the smallest to the biggest so 1 2 3 4 5 and so on so far we have learned how we can add new elements to our list in an easy way and also how we can sort the elements of our list in the alphabetical order now let's see how we can remove elements from our list and for removing elements from our list we use method called remove so you simply say shopping list. remove and then here here under quotation marks you will specify the element that you want to remove for example if I say banana banana should be removed from our list so if I start the program as you can see now inside our list there is no more banana so these are list Basics and so far we have learned how to create a list and then how to initialize that list with some initial elements and then how to add new elements to our list how to sort our list and then how to remove existing elements from the list and then here is for each Loop that we use in order to iterate through all of the items of our list and here what I'm doing is I am simply printing every element to the console so now let's see how we can apply this knowledge in order to create a useful program so what I want to do is the following here I'm hardcoding the elements that I want to add to my list and then the elements that I want to to remove so instead of hardcoding them like this I want to ask my user that he enters the element that he wants to add or the element that he wants to remove so let's see how we can create that program so let's remove this code because we will not need it anymore and then this for each Loop can stay we will use this later okay so what we have currently is we have this shopping list which has these five items initially so what I want to do is I want to create a string variable I will call it user input like this so what I want to do is I want to ask my user to enter his own ingredient and then I will remove that ingredient from this shopping list and instead of doing that just once what I will do is I will use a loop and I will do that as long as there are items in inside this list so as long as the count of this list here is not zero so here I will say while and the condition will be the following so while shopping list. count is not equal to zero so as long as this condition here is true as long as my list is not empty what I will do is the following I will print this list list okay so let's add an output here let's say console. right line and here I will say this is your list okay so I will print my list and after the user can see the entire list then I will ask him to enter his own input to enter the element that he wants to remove so here I will say again console. right line and here let's say item to remove okay so then I will say user input is equal to console readline so whatever the user types into the console I will read that and then store it inside this variable here and then I will simply say shopping list. remove and the item that I want to remove is this one that the user has entered and we will repeat this code as long as this condition here is true which means as long as our list is not empty so in each iteration we will print the entire list and then we will ask our user to enter his input and then we will remove that element from the list and do it all over again as long as the count of the list is not zero so let's start our program okay it says this is your list and here we have five items that our list initially contains and here it is asking to enter the item that we want to remove so if I say banana and press enter again it says this is your list but this time we don't have banana in our list because it was removed and then here it is asking again item to remove so if I say for example milk as you can see milk is no longer part of our list now there are two things that I would like to do here in order to optimize the user experience of this program so the first thing is this part here it says item to remove and then we are entering that item in the next line so what I would like to happen is I would like to enter that item in this same line so how can we achieve that well here where you are printing item to remove to the console instead of saying console. right line you simply say console do right so now if I start the program as you can see it says item to remove and I will enter that item in the same line so here I will say serial and as you can see it stays in the same line and here we get our list again and again it asks us to enter the item that we want to remove so that is the first thing that will optimize the user experience a little bit and then the second thing that I want to do is I don't want to have multiple lists on the screen so I don't want to have this list here and then this list here because it is very hard for the user to understand what is happening happening so instead what I would like to do is I would like to have my current list printed always on the top of the screen and then below it I want to ask the user to enter the item that he wants to remove and when he removes the item I will refresh the entire screen so that again at the top he can see current list and then this question to enter the item that he wants to remove so how can we do that it is actually very easy so what you will do is after you remove the item that you need to remove you will simply say console do clear like this so that is going to clear your entire console before it starts the next iteration so now if we start our program again here we get our list and then it is asking us to enter the item to remove so I will say milk and when I press enter as you can see this entire screen is refreshed and the milk has this disappeared from the screen so if I say here for example sugar again that item was removed and this way it is much easier and much more user friendly for your user to understand what is happening so this is an easy program that you can create and that is going to remove items from your list based on what the user has typed you can also adjust this program so that instead of removing elements from the list it adds elements to the list so let's see how we can do that so here instead of saying shopping list. remove I will simply say shopping list do add and then I will change this text here so I will say item to add okay so now if I start the program let's see what is going to be the behavior so if I say onion as you can see onion will be added to the list if I say tomato tomato will be added to the list as well and then let's say orange lemon and so on so this is an easy way to add elements to your list programmatically so now that we know how to both add and remove elements what I want to do is I want to create a program that is going to do both so when my user enters his input I will check if that item exists in my list and if it does exist then I will remove it from the list but if it does not exist then I will add it to my list so let's see how we can do that so as I said if our list contains the item that the user has entered then we will remove it from the list but if our list does not contain that item then we are going to add it to our list so now the question is how can we check if the list contains some item or not well it's actually very easy to do that so here after the user enters his input I will simply say if shopping list do contains user input this is the method that you use in order to check if this list here contains this item so if it contains this item what I want to do is I simply want to remove it from the list else meaning if it does not contain that item then I want to add it to my list and this here is all of the code that you need I will just make one small change here so I will say enter your item so after the user enters his item then I will check if that item exists in our list if it does I will remove it and if it does not then I'm going to add it to my list so now let's test this program okay so here is our list initially now let's add an item that doesn't exist let's say tomato okay and as you can see tomato was added if I say apple apple will also be added to my list but if I say for example milk milk already exists in my list so when I press enter milk will be removed from my list if I say banana banana should also be removed so enter and as you can see banana was removed so now if I enter for example milk again as you can see milk will be added again at the end of my list now even though our program works we still have one hidden bug here and that is the following if I enter for example sugar but I enter it with all capital letters and I press enter what do you think will happen and as you can see now we have sugar here and sugar here this one is with lowercase and this one is with capital letters the same way if I enter for example sugar like this and I press enter we will get another sugar added to our program and the reason for this is because for your application these three elements are not the same thing so even though for you and me sugar written like this and like this and like this is absolutely the same thing but for your program it is not because it makes a difference between lowercase letters and uppercase letters so U lowercase is not the same as U uppercase so now because of this we will have duplicate items in our list and we need to figure out a way to fix this problem so that we don't get these duplicate items so if the user enters sugar it doesn't matter if he enters it with capital letters or lowercase letters if sugar exists in our list it should be removed so we should not add duplicate item we should remove existing item so the easiest way to fix this problem is to make our program always work with lowercase letters so regardless if our user is typing capital letters or lowercase letters our program in the background will always work with lowercase so how can we do that well it's actually very easy so here when I'm adding the item to my shopping list I will say whatever the user has inputed please convert that into lower so I'm using this to lower method to convert this text into lowercase letters and then when I'm removing I will also say to lower okay and also when I check if that item exists or not I will also convert it to lowercase like this so with these three little changes we have made our entire program to work with lower case letters which means that when we check if the item contains we check in lowercase when we remove we remove in lowercase and when we add new items we convert them to lowercase and we add them as lowercase letters now the only thing that can make you a problem potentially is if you have here items that are written with capital letters so please make sure to check your initial items and make sure to write them all initially with lower case letters so you should not have chocolate written like this for example or banana written like this or any capital letters in your list initially so everything should be lowercase and then your program is going to work without any problems so now if I start the program let's see what is going to be Its Behavior so if I say for example onion but I type it like this so lowercase o and then capital letter n i i o n like this and if I press enter it will be entered in my list but everything will be in lowercase letters and then also when I decide to remove this item if I say for example onion like this so all capital letters it is still going to be removed from my list because as I said in the background everything is working with lowercase letters and also if I say banana it will remove banana and then sugar milk like this and let's also remove cereal and chocolate Okay and then chocolate and as you can see now our program is going to stop because here this condition says that this Loop is going to run until your list is not empty so when your list becomes empty this Loop will end and since we don't have anything after that that means that our entire program is going to stop now if you don't want your program to stop when your list becomes empty you can change this condition here so you can make this while loop to be an infinite Loop so you can simply say here while true so while this condition here is true and since we have hardcoded it here then it will always be true so that means that this Loop here will run forever so if I start my program this program will run Forever Until I press this close button manually so now if I start my program I can empty this list completely so let's say chocolate and then banana sugar milk cereal okay and as you can see your program is still going to work so that you can add new items to your list so if I say for example Apple Apple will be added if I say milk again milk will be added and then again if I decide to empty my entire list again the program is not going to stop so that is how you can work with lists in programming if you have any questions or any topics that you would like to see in the future let me know in the comment section and then if you enjoyed this video give it a big thumbs up so that I know to create more videos like this in the future also if you like learning from my YouTube videos then you are the person who is going to benefit the most from my practical programming course because there you are going to learn everything that you need to know about programming from beginner to expert so that you can start a successful career as a software developer I have created all of the video lessons that you need to watch and all of the exercises that you need to do and I will be there through the entire process to help you and to answer any questions that you have so that your progress is optimal and so that you can get a job and start a career as soon as possible if you have any questions about the course you can leave them in the comment section and I will make sure to reply and if you decide to join there will be a special discount coupon code that I will leave in the description so that you can save some money so thank you very much for watching and I will see you in some other video bye