 Welcome back everyone. My name is Brian. And in this video, we're going to cover lists. A list is a complex data type compared to what we've been talking about. It's very complex. So far we've covered bulls, numbers and strings, but the list is well, vastly more complex as we're about to see. So first off, what is a list? Well, it's an ordered list or collection of data. And what do I mean by that? Imagine we add an item, that item is going to be at position zero, meaning the first item you add is always zero. And as we add items, you guessed it, that index will increment. Go ahead and add an item here. So zero, one, two. And these items, they can be the same. They can be different. They can be something totally crazy. Whatever we want to do. This is the beauty of Python. So the list doesn't have to be what's called typed, meaning you don't have to have the same data type. That's how a lot of lists work in a lot of different programming languages. You can intermix strings with numbers and custom data types. The main takeaway before we dive in here is that this is an ordered zero based index, meaning it will be added in the order you add them. And the first item is always zero. From zero, it goes to one and two and so on and so on. Okay, let's go ahead and create a list. First thing you'll notice in our notes here is I have these square brackets right here. What that denotes is that it is a list. It's important that you remember that because we're going to talk about other data types that are very similar to a list, but they are different. And they use a different style. So first things first, let's create our list. Remember the square brackets. I'm going to say X equals square brackets. And anything we put inside the square brackets becomes part of the list. For example, I'm going to say Brian. And we're going to add another item by I've seen comma and then whatever we want to put in here. So we have two strings in there. Now, really hurt your brain. If you're coming from another language, I'm going to add a number. That's right. This is not a typed list. You might be used to something like C++ where, well, you have to have a specific type. It can only have strings or only have numbers. Python is not like that at all. It makes it very flexible, very easy. So let's go ahead and put a note in here. We can mix data types. And let's go ahead and print this out. And I want to say list going to print our list out. Good old copy and paste. Let's put this down here. And let's get the length. So you can see right off the bat, our list is this right here. And it's inside those square brackets. Always remember square brackets or lists, and we can have two strings and a number we could add whatever we want. And there are three items in there. If you're coming from another language, this may actually hurt your brain because you're going to go, wait a minute, you can't have multiple types, but with Python you can. It makes it super convenient, super easy to create your own data structures on the fly. Let's make a special note here about indexing and positioning. So this really confuses a lot of people. We think like, well, humans, the first item is always one. Well, computers don't think that way. The first item is always zero because you're starting with nothing. So when we talk about lists, this is zero based. Let's go ahead and let's grab our friend print here. And let's say we want zero. So to grab the first item in that list, simply say square brackets and the index. Now note, we are not creating a list. We're saying inside of that list, get that index. In this case, it's the zero or the first item. I'm going to put that in all caps for you newbies out there. The first item is zero. Remember, humans think the first item is one. Computers think the first item is zero because you're starting with nothing. Now, if you watch the previous video on strings, and I hope you did, you understand what slicing is. But just in case you skipped it, a slice is a lot like making a slice of bread. You have a nice big loaf of bread in front of you, and you don't want the whole loaf. You just want a slice of it or a slice of pie is probably a better example. So we're going to print this out and we're going to slice this. So I'm going to say I want X and then we're going to you guessed it. Now notice those are square brackets as well. We didn't cover this in the last video, but yes, when you see that square bracket, that means we want a piece of this list or in this case, a slice of it. I'm just going to put some quotes here or some quotes, some comments, slice the list. So what this is going to do is it's going to say X and we have a starting position and an ending position. We're going to go from the first to the second. Now remember, the first is not the first item. It's position one. Remember that gets super confusing as far as zero based indexes. To illustrate that one to two position is actually Karen's. So what we're doing is we're taking this list here, and we're saying zero one to two. So it's going to stop. If we omit the second position, we're going to get the remainder of this. We're going to say we want that whole slice right there. So let's demonstrate that works as expected. Slicing is a very convenient way of getting a sub item. You may be thinking lists are great, but now that I've created the list, how do I add items to it? That's a common question. So that's what we're going to cover in this little section here is how to add the items and you can either append or insert. And both of them do well exactly what they sound like they do. For example, X append, we are going to say append pizza. Let's go ahead and X dot append. I'm doing this twice for a reason and you'll see why here in just a second. Add beer because everybody likes pizza and beer right now we want to insert. So append will put things at the end in the order you put them. Notice we did two of pads, pizza and beer. So it's going to add pizza, then it's going to add beer and it's going to do both of those at the end. Now insert allows you to say I want to put this at a specific position. So because we're talking about a specific, I need to give it a number and in this case one, you notice it wants an index, an integer of where you're going to put this in the list. So I'm simply going to say one and let's add cats. So following along what we're doing here is we're adding pizza to the end of this list, then we're adding beer. Then we're going to go to the one position, not the beginning. The beginning would be zero. Remember, we're going to go to the one position and add cats. Let's go ahead and add a few notes here. That way anybody who downloads this code off GitHub will just have the notes and be able to easily follow along. Now let's go ahead and do a print. And I want to see this list in its entirety. And let's see what this looks like. Okay, down here you can see we have our list and we have Brian and then cats got inserted in the first position. And then my name Karen's 46. And then it did pizza and beer. Very important to understand that append will do it in the order you append it. So it put pizza at the end of the list. Then it put beer at the end of the list. Very simple. Once you wrap your head around that just trips up a lot of newbies. Just bear that in mind when you're adding things. If you want a specific index you need to do insert. If you want to slap it at the end, you do append. Okay, now that we've added items let's talk about how we get rid of items. We're talking about removing. Let's remove pop and delete. And these do things very differently even though at first glance they may seem very, very similar. So I'm going to say x.remove. And I want to remove cats. I'm not a big fan of removing cats because I happen to love cats but you know what I mean. Sometimes you just got to get the cats out. So what remove is going to do is go through and remove the first item here. Let's grab our print statement, slap that down here and let's run this. Alright, so you can see Brian cats. Karen's now it's Brian Karen's we've removed cats. So just remove that first item there. Now let's talk about pop. Now don't get excited pop is not like Coke or Pepsi or anything like that. But what we want to do is we want to remove an item but at the same time return it meaning we want to be able to work with it here. So I'm going to say I equals and we're going to do x.index because we want to find something here. We want to find pizza. Now this will raise an error if it's not found in there. So if you just look for something crazy it's not going to return negative one. And there really isn't a find remember we talked about find in the last video where it returned a negative one. This is just going to say hey there's an error and it's going to crash your program. That sounds really bad until we get into more advanced discussions where we talk about how to get around that and why it even exists in the first place. So just big leap of faith at the moment. We know that pizza is in there because we appended it. We're going to get that index because nobody wants to hear and count and figure out what position this is actually at. We're going to say food and we're going to pop. Now when you think pop think of it like popping the top off of a nice cold soda or beer whatever your favorite beverages. You now have that cap in your hand after you've popped it off. That's at least how I learned pop. So when you pop off the cap you still have that cap in this case it is the item at index of whatever pizza's at. Sounds super confusing. It really really does. But it's really simple once you see it in action. So what we're going to do here is I'm going to clear this list just to avoid any confusion. And we're going to run this. Boom. So what's going on here we have our list and see there's pizza in there. And we're going to pop meaning we're going to pop the cap off that thing that's in our hand and we now have that bottle cap. And that bottle cap is pizza so we can now use that. But once we've popped it it's no longer in the list see how it removed it. Very convenient if you want to remove the item but at the same time use the item. Seems a little confusing but bear with me. That is actually used quite a bit when you get in a little bit more advanced programming. All right next up we're going to talk about deleting. So I want to say I equals say X index. And let's get the index of beer because again no one wants to sit here and go zero one two three. No one wants to do that we just want the computer to figure out where it's actually at for us. So once we get that index and I should put a special note here. This will raise an error if not found. But once we get that we want to actually just delete it. So I'm going to say D L X and then whatever that position was. Now this will delete the item without returning it and I'm going to copy and paste some notes in here. So delete a specific item without returning it where pop will remove and return the item. Fundamental difference there. Which one would you use and why well pop is if you want to actually use it delete is if you want it gone. Now when you delete this it's gone you cannot use it it doesn't even exist in memory anymore. And that's why it's called delete because you're literally deleting that out of memory. If you're coming from like C or C++ or any other language that does memory management. Yes you are actually deleting that out of memory. Now under the hood what's really happening is that you're actually deleting that out of memory. Now under the hood what's really happening is pythons flagging that for garbage collection and all this other fancy stuff that newbies don't really understand. But if you're more of an advanced person that's what's going on is Python saying get rid of it out of the Python internal list. We're just going to let garbage collection wipe it out later on. Save run this is what's going on here so we found the index of beer and we deleted it. Pretty cool pretty easy to understand so major takeaway from removing items if remove will remove the first one pop will remove it but let you play with it. You can do whatever you want and delete will well actually delete it and get rid of it. Let's talk about extending or adding elements from another list and this will be a little bit confusing but bear with me here so. There's a reason why you would extend rather than just add a list and it's because Python you can actually add less in the list which we're going to cover at the very end of this video but it gets super confusing. So let's look at this so we're going to say we're going to make another list and let's call this cats. Mama dogs. Mama birds and in case somebody out there is like well how would you actually add a comma you wouldn't add a comma directly you'd add it as string like that. But we're just going to add in cats dogs and birds into our list called why now the list we've been working with is called X. So we want to take the contents of why and put it inside of X. So we're going to say X dot extend. Sometimes IntelliSense helps us out sometimes it doesn't. And we're going to extend with the Y list. Let's go ahead and print this out. Notice we're printing X. What do you think pop quiz what do you think this is going to look like when we run this. Well if you said it's going to have the contents of both lists you're absolutely correct. Now notice how what it's done is it's taken our X and added the elements of Y to it. So these are now true items inside of X. Very cool how that works. So let's talk about sorting. And when I say sorting we mean sorting and reverse sorting meaning doing the exact opposite of like an alphabetical. So let's go ahead and say sort and reverse. Now think of this as like sort ascending sort descending. Now our list is a mixture right now and what do I mean by a mixture. So if we just take this print this out run it. You can see how we got string string int string string string. We're going to have problems with this integer and we're going to have to take that out and I'll demonstrate this really really quickly. So I'm going to say X dot sort. And this is where a lot of newbies get tripped up and it goes not supported between instances of int and string. And it's exactly that. So it's saying it knows how to sort strings. And it knows how to sort numbers but it doesn't know how to sort a string to a number. So think about that. Let's say you have a table in front of you and I come in and I dump a bunch of potatoes and a bunch of apples and I say sort these. Well how do you want them sorted do you want the potatoes in the apple sorted or do you want them sorted by color but they're all different colors and this is what I mean. Python gets really confused and it just says you know what I'm not even going to try and figure this out. I'm just going to drop an error. Let you just figure it out on your own. The easiest way to get rid of this at this point is to simply remove it. So I'm going to say X dot remove. We're going to just remove that int and then we're going to sort. So now it's alphabetical because we've removed that it knows exactly how to sort these strings. Birds, Brian, Karen's cats, dogs. Very, very simple. The polar opposite of course is reverse and we're just going to grab this for the sake of time. Pretty self-explanatory what's going on there. This would be like sort ascending sort descending. I almost wish they didn't call it reverse but called it like sort descend or something like that or you know had a parameter inside of sort. We'll talk about parameters later on but it can be a little bit confusing but just think of sort as you're going to sort alphabetical and reverse is the polar opposite of that. You notice how when we sorted we had to remove that int simply because sort will throw some sort of error if there's a mixed type. Now what if you wanted to do that but you didn't want to really modify the original list. Well what we're going to do now is call it copy and some people call this cloning which is a very shallow clone but basically we're just making a copy here. So we're going to say y equals x dot copy does exactly what you think it does. It actually makes a new list and copies the elements into it. So we're going to say copies the elements into a new list. Now we can simply say y dot reverse and we could add items, remove items, do whatever we wanted to do. So I could say y dot append. Let's go ahead and append apples. We could do any other thing we really wanted to do here. So let's grab this print statement here. So x is the original new is our y. Let's see what these look like here. So our original remains unchanged where our new now has apples and it's been reversed. Pretty cool the way that works and then if you were done with wine you didn't want anymore just simply delete it which is what we're going to talk about next. Okay so deleting. Delete the whole thing is really what I'm going to put here. Why would you want to delete something? Well in the previous little segment here we said we're going to make y and y is a copy of x. Now that we're done with y we don't need it anymore. We can do one of two things. We can either let Python garbage collect it meaning it's going to figure out when it's not needed anymore. Or we can specifically say delete the whole list. And yes that deletes the whole thing. So if I try to say something like print y it's going to have a bad time here and it's going to say name y is not defined. And you remember from a previous video whenever you see is not defined it means it simply does not exist. Meaning we have now destroyed y and everything in it has been destroyed as well. So all that memory for your computer nerds out there is now freed up and gone. We can use it for other things. This really becomes important when you start talking about embedded systems and things of that nature where memory might be a little bit more constrained than say your desktop or your laptop. You got to understand that when you delete something it is gone. And I mean gone gone is not defined means you would have to rebuild this entire data structure from scratch. There's simply no way to recover it. Deleting can be a little bit drastic as we saw name y is not defined is well kind of a problem. I mean what if you didn't want to delete the entire variable you just want to delete part of it or clear the whole thing off. That's what we're going to talk about in this little segment here. So we're talking about clearing think of your list like a big table and you got plates and dishes and all sorts of stuff on there. You don't want to throw the whole table out you just want to clear it off and that's exactly what clear does. So it's going to clear all of those elements out of the list but the list itself will still exist. It will just be blank to demonstrate that those type cleared. So most of the time in programming land you don't actually want to delete you just want to clear it out. So our list exists you can see by these brackets there's just no elements in there and we can actually test that. Let's go ahead and get the length of X. So it exists but there's no items in there. Most of the time if you're confused just think of it this way. Delete is throwing the entire thing out you're throwing the whole kitchen table out where clear is you're just clearing the dishes off the table. Usually you want clear not delete but delete exists in case you need to free up that memory. You want to do something a little crazy a list can contain other lists and if you look at this it looks kind of weird. But just think you have a start bracket and an end bracket followed by start and end start and end and start and end. So that's right a list can contain other lists if you're coming from another language think of this like an array of arrays. It's kind of advanced and it's not something a newbie really understands but in case you're curious this is how you would do it. So we're going to say X equals and we're going to turn X into a blank list with nothing in it. Y equals we're going to create another list we're going to say one two three and let's go ahead and make another list called Z. And in here I'm going to say my name you go ahead and put your name or cats dogs birds whatever you want to put in there. So now we have three different lists. What we want to do is we want to take the contents of Z and Y and put them in X. So I'm going to say X dot append. Let's go ahead and append Y. Now what I've done is I've taken all of these numbers and put them inside of X. Let's go ahead and print this out. Now I'm saying merged but that's not really what we're doing here. We're actually merging all these into one giant data structure. You notice how we've got these double brackets here. So what we're saying is this item this individual element is its own unique list. So I'm going to say list in the list. All right now what we want to do is we want to take the contents of Z and put them in X. But we want to put them before all the other stuff that we put in there say X insert. And let's go ahead and put this at the zero index and we're going to put all of Z in there. Let's go ahead and run this. And you see now our list actually is made up of two smaller lists. Newbies may look at this and go well there's five items in here. There's really not. Let's go ahead and test this out here. We're going to say grab this and let's call this our list. And let's go Lynn and we're going to get the number of items in here. There's only two items in there. Very simple very easy to understand. Now let's say we want to grab the first item. We want to say zero and we want to go zero. Let's go ahead and grab that second item. So our first item is this list here which is its own little list and second item is this list here. And in case you're wondering yes you can get kind of nuts here. What we're doing now is we're diving into those lists. So in our first list right here we're going to grab the first item which is Brian. In our second list we're going to grab the second item notice that's at the one position which is two. Seems super confusing but just think of it this way think of it like a stair step. Every time you see these brackets you're going deeper down a step into the basement. So for example we could switch this around here and we can grab the very first item which was one. We could switch this around again and say we want the second item which is three. Seems super confusing if you're a newbie but trust me once you get into more advanced stuff this is a huge huge time saver. We're putting lists and lists and when you start working with things like web services you're going to find that quite a bit where the data they're going to hand you is basically a list of lists and you just simply have to navigate through it. Okay a little bit of bonus material here I know I'm going to get the question so I thought I would just add it. How do you change an item remember it's positional. So for example let's say x equals and let's just make some numbers here one two three four five. We want to actually change this. We don't want to change the whole list just one specific item. So what I'm going to do is I'm going to say x and then whatever the item number is. So let's say I want to change the item at position two. And we want to change this to the word test. Remember this is zero base and this trips up a lot of newbies so we have zero one two so this guy right here we're going to change that. Let's go ahead and print this out and let's see it in action. There we go zero one two are two position is now test four five so that is very simple how you change something. A lot of people especially me take this for granted because it is so simple and easy to work with. And if you've worked with other languages you may be looking for like a change element add or swap element or something like that. It's actually very simple. You take the list you take the position and then you assign it a value. I hope you enjoyed this video. You can find the source code out on github.com. If you need additional help myself and thousands of other developers are hanging out in the void realms Facebook group. This is a large group with lots of developers and we talk about everything technology related not just the technology that you just watched. And if you want official training I do develop courses out on you to me dot com. This is official classroom style training. If you go out there and the course you're looking for is just simply not there. Drop me a note. I'm either working on it or I will actually develop it. I will put a link down below for all three of those and as always help me help you smash that like and subscribe button. The more popular these videos become the more I'll create and publish out on YouTube. Thank you for watching.