[MUSIC] You've been introduced to lists. Now I want to walk you through some examples using lists. I think one of the best ways to understand lists is to see them in action. We're going to do this in the context of the kinds of programs that you're going to be asked to write for this class. So you're going to get to see how simple GUI programs use lists. And how they interact with the kinds of elements that you're going to see in an interactive program. Now by the way, can anybody help me finish that last task on my to do list? Anyway, I hope you find these examples helpful. And I hope that it allows you to write better, simple GUI programs. All right, I want to start with the simple program that we used for mouse input that allowed us to put balls on the screen. To briefly refresh your memory, okay? I click around here. The ball moves around. I can touch it, get it to turn green. Go back to red, okay? I want to extend this program to actually use a list of balls. So, instead of having one ball, okay, I want to change this program, To have a list of balls, all right? Okay, so I want to start out with an empty list. And every time you click, I want add a new ball to the system. So, if I click, what I'm going to do, all right, let's ignore this stuff for now. And instead, do the following. Ball_list.append(pos). Okay, so I'm going to append the position into the ball list. And now I have a new ball each time I click the mouse on the screen. But that doesn't quite work, right? If I run this, let's see what happens. I got a name error, ball_pos, right? Because I'm trying to draw that old one. So I have to introduce a new concept here. I've appended to that list. That list now has, potentially, many elements. After the first click, it'll only have one. I have to figure out a way in here to draw every single ball. Now, I can't just write canvas.draw_circle over and over and over again, right? Because I don't know how many there are. So I have to be able to run through the list, one at a time and actually draw each ball in turn. This is called iteration. And in Python, iteration is accomplished with for loops. And the syntax is actually pretty simple. I'm going to say for ball_pos in ball_list. Now notice this in here is not the in that I discussed previously. The in I discussed previously was a check. What if you're doing an if statement? If ball in ball_list would return true or false depending on whether the ball was there. For ball_pos in ball_list actually says run through ball_list. And for each element in the list, set ball_pos to be that element. And then do whatever it is in this indented block, right? Fors end with a colon, I have an indented block that gets executed. Okay, so now let's try this. If I do this, okay, that's good. My program didn't immediately get an error. I can click, and bam. Now I get as many balls on the screen as I want. I can keep going, okay? So with two very simple constructs. Just append and a for loop, I can now extend this program to be much more interesting. And allow me to have all kinds of balls all over the screen. All right, now I want to change this program to make it a little bit more like the original. What I want to do now is when you click in an empty space, it adds a new red ball. When you click on an existing ball, it turns that ball to green. So let's think about this. Each ball is now going to have it's own color. So I can't have this global variable that tells you the color of the single ball in the system. Similarly, I'm not going to just append a ball to the list blindly. First I need to check, do I want to? I'm not going to use this code. Okay, so I want to check the distance. But I can't check the distance between the click and a single ball. I have to do it between a click and all the balls. So this is another prime location for a for loop. For ball in ball_list, okay? Check the distance. Now, I want to point out that because each ball is going to have its own color, I don't want to have a ball be a list of two elements. I want to have a ball be a list of three elements. So instead of just x and y, I'm going to store x, y and color. Okay, so to check of distance, I have to create a new list that just has the x and y position in it. Now that will check correctly. I don't want to check that color that way. I just want to blindly set the color, which will be the second element to green, okay? So, now I'm going to run through every ball in the ball list. I'm going to check the distance between the mouse click and that ball. Make sure if it's less than the radius, I'm going to turn that ball green. When I'm done, I'm going to append a new ball to the ball_list. But, hey. I only want to do this if I didn't actually change the color of any existing ball. So I want to add what I'm going to call a flag, which is just a Boolean value. And it's going to start out false. I'm going to say changed = false. I have not changed the value of any balls or the color of any balls. If I do, I set that flag to True. Now when I get out here, I can just test it. If not changed, meaning if none of the balls had their color changed, now I can append one. And I can't append just pos because a ball is now 3 elements pos. Which is the x position, the y position and the color. Okay, so there we go. Now the click handler will run through. Turn things green if I click on top of a ball. If I click outside of all the balls on the screen, it will add a new one. Over here in the draw handler, we have to update this to reflect the fact that our ball is now a three element list. The position is the 0 and first, and the color is the second. Okay, now this should work. I should be able to click around here and get new balls all over the place. If I click on top of a ball, it turns green. If I actually place a bunch of balls really close together, I can click on the intersection of them and it turns them all green, okay? Now, I don't want to explain why multiple balls turn green. I want you to actually go back, and look at the code. and make sure that you understand why multiple balls turn green with one click, all right? But otherwise, hopefully this program is somewhat intuitive and you can actually see the power of lists. As a final variant to this program, actually I want to try and remove balls from the screen. So now, instead of changing the color to green when you click on a ball, I want to remove the ball. So now I no longer need the color. I can put ball_color back to be red. Okay, let's do that and that. Balls can now just be two element lists. So what am I going to do here? For ball in ball_list, if I get the distance, I don't need to do this anymore. Okay, instead of turning it to green, I want to remove it. So what do I do here? Now the problem is when I'm iterating in a for loop over a list, you cannot take things out of that list while you're doing it. And this is an error that many programmers make, okay? So instead of doing that, what you want to do, Is create a new list of the things that you want to remove. Okay, so if I want to remove this, I append ball. This is telling me, hey, later on, remove this, okay? Now I can say, if remove is an empty list, then I know I want to append it. I don't need to append this three element thing anymore. I can just append the position of the mouse click. Else, now I want to actually remove things from the list. So how do I do that? I will need to iterate over the list again for ball in remove. Now I'm iterating over the remove list now, not the ball list. So I can say ball_list.pop(ball_list.index(ball)). Okay, ball_List.index(ball) finds the ball in the list. And gets you its index position. And then ball_List.pop removes that ball at that spot, okay? So if I do that, we run this program. Click, click, click, click, click. Bye-bye, right? And if I can successfully overlap balls, I can click right here, get rid of all of them, okay? I don't want to explain this any further. I want you to think about it. I want you to look at the code and see if you can understand it. Play with it, modify it, see what happens. But the key lesson here is, never remove something from a list while you're iterating over it. Okay, if you need to do that, keep track of the things you want to remove and remove them later. So hopefully you learned something about lists from this video. I want to reiterate that this is not all there is to lists. There's a lot more. So I hope you'll read the documentation and gain some more familiarity. Most of what you need to know for this class, if not all though, was in this video. So hopefully, you have a firm foundation now to do the things that we need to do from here on out. Now, you may not have noticed but I'm actually a professor. And sometimes, professors can be a little bit sneaky. I snuck a bunch of things into this video that have nothing to do with lists. You may have notice that I showed you the same program over and over and over again. What was going on there? This is actually how you write programs. I iteratively developed that program and I changed its behavior. And you notice I kept it working. Okay, if you follow this process, you're going to be in good shape. Where you start with something simple, you modify it a little bit, keep it working, run it again. Modify it a little bit, keep it working, run it again. Modify it a little bit, keep it working, run it again. And finally get to the final result that you'd like. You notice I didn't make that many errors too, right? Okay, this is the way it works. If you do small changes, you end up with a much better system, okay? I also showed you a little bit about data structures, right? I told you about keeping the color of the ball directly with the position. Keeping that data together made it a lot simpler. You could try doing something else. I encourage you to do so. It's going to end up with a lot messier program. You won't be able to get it working nearly as quickly as I did, okay? Now, here's a question to ask yourself. Have I been sneaking stuff like that in the rest of the videos?