[MUSIC] Lists are the work horse of Python. You will probably never see a Python program of any complexity that does not use lists. We introduced these lists last week, but we showed them in their most basic form. We showed fixed length lists. And you could access them, you could modify them, but that was about it. Today we're going to talk about some more of the capabilities of lists. You can actually dynamically add and remove elements to a list so that its size changes while your program executes. There are a lot more methods to list as well. But we're going to show a few today. We're going to show the ones that you need for this class. But I encourage you to look at the documentation and learn about the other features of lists. With that, let's get started. Okay, so let's take a look at what we can do with lists, right. So I can create my list here with the brackets, just as we did the last time, and let's put 1, 82, -6, 4, 3 and 8 in my list. Okay, so I have a list, I can create it this way. I want to talk about some additional methods. We know how to access elements, right? If I want to print lst [2], oops, that's a bracket. We know that what will happen is we'll print -6. I know that I can get to the last element, I can print lst [-1], get to 8. But what do we do if we want to do something else, and something more interesting. I'm going to talk about two methods that allow us to examine things inside a list more easily that's in and index. So what do these things do? In allows me to check if something is in the list as the name kind of implies here. So I can write statements like 82 in lst. And this will evaluate to H/Z. Now that was just checking if you guys were paying attention in previous lectures. It will evaluate to true or false, okay. So in this case 82 in list, well here's 82 right here, that's going to evaluate to true. So I can write statements like if 4 in lst print "4 is there!". Okay, so you can do checks like this right. Index gives me a little bit more information. This just tells me whether or not it's in the list. Index tells me where it is in the list so I can write statements like, let's say lst.index (8). Okay, and what this returns is the location in the list. So we've got zero, one, two, three, four, five. This is going to return 5, okay? I could similarly do lst.index, should spell it correctly, okay, 4. And I will get 3 returned. Okay, so in an index giving me more information about the contents of a list. I can figure out if something's in there and if it is I can figure out where it is in that list. Now that's interesting and that gives us a little bit more capability beyond just looking at individual elements of the list. And it allows us to use lists for additional things where I can check for membership. Still I'm stuck with my list being a fixed size. So I want to talk about two additional methods, append and pop. And this is where things really start to get interesting. With append and pop I can now change the list okay. And we saw how I could change it before by just changing an element. So I could say something like lst [2] = 5. Okay, that would go through, that would come over here, that would actually get rid of that. Going to replace it with a 5, okay, and that has changed the list. Append and pop are a little bit different. I can now say lst.append (632) and what this does You'll ignore that craziness. My eraser works. I can actually now add the 632 to the end of the list. My list grew, my list got longer and I can put a sequence on these appends. I can make it as long as I'd like. I can also call lst.pop() and that actually now removes that last element so my list will go back to looking like this. If my pen would work, here it is. Okay, so that removes an element. So I can shrink it, and I can shrink it back down to zero elements in there, if I like. Lst.pop actually returns something, and what it returns is the element that was popped. So in this case it would return 632. So with sequences of appends and pops I can change the size of a list, within an index I can find things inside the list, or I can find whether or not things are inside the list. Now if pop only remove from the end it would be a little bit restricted. So pop actually takes an argument that the default is the end, but I could say list.pop(4), and that would remove the element at location four. Zero one two three four, so rather than erasing them I'm just going to scratch it out. Now that's gone, okay. With these methods I can do a lot more with less. And as I said before there are additional methods that I can use on lists and I strongly encourage you to go to the CodeSkulptor documentation and explore them. So let's look at these list methods in action. Let's see what we can do with lists and I want to use a task manager as a simple example of how to do this. So here's my little task manager. I have an empty canvas right now. I have an input for new task. I have an input that allows me to remove a task by number and I have an input that allows me to remove a task by name and a little button that clears everything. You can see the code over here. I've hidden most of it, I'm going to reveal it as we talk about it. Okay, so the new task, what happens here. I start out with a global variable, tasks, that has no tasks in it. And you can see over in my canvas no tasks are being drawn. So let's add a task. What kind of task do we have? Right, what do I need to do today? Well, I need to teach Joe how to play Pong. Okay, there's my first task. All right, and you can see I'm going to ignore the draw handler for this entire lecture. And we're going to talk about that, and the things that are needed for that, in the next lecture. But, for now, it draws the tasks that I've added, Teach Joe how to play Pong, right? Let's add another one, let's say Buy some ties. I feel like my tie collection is getting a little light, I need to go get some more, okay? I can add that in. Now you can see it's adding things. So what actually happened? Let's click over here to new. Okay, this is the event handler. When I enter something into that input box, we can go down here the new task calls new. It's real simple, okay? No matter what I typed in that task box, all it does is call tasks.append with the message. And so this list is growing as we use the to do list, or the task manager, okay? That's easy enough, all right? So I could keep adding lists here and Don't forget the colon. How about Find missing Rice Owl. Okay so now I've got a bunch of tasks. I would like to be able to remove them as well, so I have my remove by number box here. So I say well, I remembered the colon, I don't need to have that tasks anymore, there it's gone. Okay, so I can type a number and my task will be removed. There's a hopeless task on here that, it's never going to actually happen. I'll be able to beat him forever now, okay? All right, so I can keep doing that, right? And my list shrinks correctly. And when it redraws the task list, only the tasks that remain happen here. So what does remove num look like. Well, first remember the input boxes, you get a string as the argument, so tasknum here is going to be string. I convert it to an int with the int function, now n is the number of the task. You'll notice that I numbered my tasks one, two, three, four, five etc. But in Python lists are index zero and so on. And so I want to check that this is actually a valid task. So it's gotta be greater than zero, it's gotta be at least one. And it's gotta be less than or equal to the number of tasks that are currently in the list or len(tasks). And if it is then I'm going to call task.pop with n-1. All right, I gotta do n- 1 because I'm trying to remove task one, I'm really removing the 0 element of the list. So there's your remove number, okay. So I have also added a way to remove tasks here. You may not have noticed, but I really quickly jumped out to the store during this video while we've been talking and came back. That's the magic of being able to use a video camera and cut things and paste. So, I don't need to buy some ties any more. I gotta bunch of ties that you'll be seeing later in the semester, and that's gone too. Now how did I do that? Let's look at remove_name, okay. Well I'm using in so if the name of the task is in the task list, so taskname and tasklist, then I'm going to remove it. The remove method, which I didn't talk about okay, I told you you should look at the documentation. There are many interesting methods and I'm just doing this as an example to show you one. Remove takes an element, it looks forward in the list and if it finds it removes it. If it doesn't find it, it's an error. So I had to do in here first, okay. So, my simple tasks list here has demonstrated to you the use of append, right. It's demonstrated the use of pop, it's demonstrated the use of in and I added remove here instead of index. And hopefully you could understand that I could have written this tasks.pop(tasks.index)(taskname), right? Then this would have figured out the index of that task, because I know it's in there because of the n was evaluated to true, and then I can pop that index, okay. So I encourage you to look at this program a little bit more carefully, expose some of the functions that I haven't exposed here, and make sure that you understand what's going on. As you can see, lists are really quite powerful. Without much code at all, I hope you'll agree that we created a somewhat reasonable-looking task list program. The methods append and pop and in and index and so on are very powerful, and this is really why lists are the workhorse of Python. We're going to look at some more examples, because I think that simple task list is probably not enough to get you really comfortable with list. And I think that's critically important to becoming a successful Python programmer.