 Hi everybody quick refresher on Python lists. So when we're thinking of lists within Python, those lists are a sequence of elements and what those individual elements are can be just about anything. On this slide I've defined a variable X and it's made up of a list and that list has Alice, Bob, Fran, John, and these are all strings as is indicated by the tick marks, but I've also shoved in there 1, 2, 3, 4, 5, which is an integer and 10.0, which is a float number. So lists can be a mix of different things and usually when you create a list you should have something pop into your head that looks like this where we've got these things in the order that they were created and then associated with each one is what we call the index which is this a particular item's place in that list, but always starting out at zero computer scientists we start at zero when we're counting right so Alice is the at index zero, Bob is at index one, Fran two, John three, one, two, three, four, five, is it four, et cetera, et cetera, okay? So this is the basics of a list and this is one way that you can create a list. So let's look at some really essential list methods that hopefully you've been exposed to before and just brush up on what they do and how they work. So I'm going to switch on over to pie charm again. Okay, let's get it nice and large so everybody can see. All right, so here's my list. Go ahead and copy this in from the slide. One thing to remember about lists is that unlike strings lists are immutable. In other words some of the methods that we're going to talk about will actually change the list that's inside of X, right? As X is done as being manipulated without having to reassign some new thing to X, okay? In many of the methods we will see apply to strings as well. So first things first, the length of the list, well what is going to be the length of the list? What do you think the length of X is? That's just a count of the number of items in the list. Six. There are six items, right? One, two, three, four, five, six. All right. Now the next important thing we'll talk about is subscripting and slicing. Okay, so say I want the first element in the list. Well, the first element in a list lives at index zero, right? And that's Alice. So if we print first here, we should get Alice, right? Alice is the first item in the list. She lives at index zero. I can also just print this directly. No reason. This X with the square brackets and then inside the brackets is the index of the item that we want to return. Think of this as a method call. That's what it is in truth. And it's saying give me the item at index whatever and return it. And that's what's happening there. In python, you can do a neat trick. Which is you can ask for the negative one thing in the list. Well, what is that? Negative one is the last item in the list. Okay? So now what I should get is a print of Alice and then the number 10 dot zero. And that's in fact what I get. And you can manipulate these. You can print out the second to last thing. You can print out the fourth item in the list. Or the fourth indexed item, which lo and behold, these just happen to be the same thing. Okay? This process, this act right here, this is called subscripting. Okay? This is the subscript. All right. We can do other things. We can do what is called slicing. Slicing is a more general form of subscripting. It's taking pieces out. So let's say we just want the first couple of things here. Okay? Let's say we want, actually you know what? Let's start somewhere easier. Let's do, we just want the last two things in the list. Okay? The numbers, these two numbers. But give me, give me what they are from this guy all the way to the end. Well what's the index of this guy? The index of this guy is zero, one, two, three, four. Okay? So let me print out four and then a colon. Okay? What I have gotten here out of the slicing operator is a list. Note the brackets here. This is returning a list and the last two items. So this little operator here, let's start, let's move it. Let's say from index two until the end and run this. So I'm getting everything from index two to the end of the list. That's what this little colon gets you. But maybe I don't want everything to the end. Maybe I just want index two and index three. All right? So what I'm going to put here is maybe a little counterintuitive. I'm going to put a four here and let's run it. What I get are Fran and John. Okay? So the way to read this is that the slicing operator is a slice from a start index to an end but exclusive of the end. Okay? So what I'm getting here is, hey, start at index two and go up two but do not include index four. Index four is this. Zero, one, two. Okay? Give me two. Give me three. Up two but not including four. Okay? So this is slicing. This is what I've got here. Maybe also you want everything in the list except the last thing. So here I've got my colon. I have not specified a start position. That means start at zero. It's the same thing as saying this. Okay? But give me up two and including everything except the item at index negative one. What's index negative one? Index negative one is the last item. Alice, Bob, Fran, John, et cetera. Maybe I want everything but the last two items. Okay? X sub with a colon negative two. Up two but excluding everything after index negative two. Okay? So maybe a little confusing. Play with it. Practice with it. It's a very handy tool in Python. It's actually kind of unique to Python in a way. At least it's relatively unique among the most popular programming languages. Very powerful. All right. Moving on. Other things we can do with this list. These methods, by the way, the subscripting, the slicing, all these methods, these next three methods also apply to strings. So you can get parts of strings like print. Let's let's do a string named abcdefg. And let's print from everything but g. Last item, abcdef. All of these operators work on strings as well. All right. Index. Okay, let's do the index. The index returns the index of the first instance of X in a list. So what is the first index of Fran? Well, Fran is at index 012. So I should get a 2 here. Just like we went over with the strings. Same thing for count. Count will return the number of items matching that match whatever you give it here. So there are, oops, I did the wrong thing. Count. So there are one aliases in here. I can also look for 1, 2, 3, 4, 5 because that's in the list. There's exactly one instance of 1, 2, 3, 4, 5 in this list. I could add another one. And it would give me two. All right. Okay. So all these methods, these are just kind of giving you some information about what's in the list. Let me copy this down here just for so we can see what we're working with. We can also modify lists. I guess I don't need that. We can also modify lists, right? Maybe we want to build a list on the fly, say as we're reading data in from a file. That's also pretty easy to do. I'm going to make a list of numbers just for simplicity's sake. So let me initialize an empty list. A list with nothing in it. But my intention here is that I'm going to create something. I'm going to put things in this list. This list is empty. These brackets got nothing in them. Why don't I build up a list of numbers? Let's say powers of 2. Okay. So how many powers of 2 do we want? Four i in range 10. How about 10 powers of 2? What we'll do is we will append here. That's append an element to the end of an existing list. Nums dot append i times 2. So where this will start and once we're done, let's print the value of this list. Okay. Oh, I'm not doing powers of 2. Am I? I'm doing multiples. There we go. A little bit odd. Not exactly what I expected there. That's because I'm being a goober. This is a power of 2. 2 to the i. Oh my goodness. This double star operator's exponentiation. It's saying 2 to the power of i. Well, i is going to go in the range 0 up to but not including 10. If you're not quite clear on how this range function works, you should look it up. It's pretty intuitive. Basically, range returns a list that is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, but not including 10. Okay. So 2 to the 10 is 512. Okay. No big deal. What's important here is that I am appending. I am appending this calculated value. Now notice the append is modifying this list as it goes along. It's building up, building up, building up. And in fact, why don't we print it as it builds up every iteration? Oops. We can actually see it. It builds up. Initially it's empty. Then we've added 2 to the 0 with power. 2 to the 0 is 1. Then we added 2 to the 1. 2 to the 2, 2 to the 3, etc., etc., etc. Okay. So appending modifies our list. Pretty neat. Very useful for, again, whenever we need to build up a list by when we're like, for example, reading in data. Another thing we can do is we can insert items into a list. So right now, my list looks like this. Maybe I want to put the number 3 in here for some reason. Maybe I'm working on a Fibonacci sequence or something. Insert, the way it works is you insert at the index you want. Okay. So first we give it the index. And I want to put 3 right here. I want to put 3 where the 4 is and basically move all these guys down one. Okay. So what I want to do is give it the index of the place I want to insert the number 3. 0, 1, 2. I want to insert the number 3 in order. So I want to insert at index 0, 1, 2, right here, the number 3. Actually, let me put something else there. The name Bob, index 2. There's Bob, right? So I've actually, again, I have modified the list by calling this insert function 1, 2, Bob, and then everything else is there. It's just shifted down a spot. Okay. This does not return anything. Okay. Inserting does not return anything. And if I print this, it will show me none. The word none. That means it's not returning anything. It's modifying in place the list. Okay. Remove. All right. Well, putting Bob in there was a mistake. So let me remove Bob. Okay. When I remove Bob, I inserted Bob and then I removed Bob. He gone again. Okay. Note that remove only removes the first value here that it finds. And if you try and remove something that's not in the list, like Alice, you will get an error. You will get an exception. Okay. So Python will scream at you. All right. Two more methods I want to show you. Popping. Popping you may not have encountered. We will use this a little bit. What popping does is it removes and returns an item by its index. Okay. So let's make a new list. Let's go back here. Let's get this guy. Let's go back and work with this list again. Suppose I want to get rid of the very last element in the list. I don't care what that element is. Just get rid of the last thing. Okay. I can call x.pop. That's going to remove it. And if I print x after this, let me get rid of the stop printing the rest of these folks here. If I remove, run this again. I remove the last element of the list, which is 10. It's gone. Okay. Not to be found. I got rid of it. I can also remove arbitrary things from the middle. Say I want to get rid of Fran. Fran is at index 0, 1, 2. Let me pop 2. It's going to get rid of Fran. Okay. So this is useful. Pop and pop with an index is useful when you want to remove, you know where an item is, but maybe not its value. Whereas remove, you have to know the value of the thing you're trying to get out of there. Maybe you just want to chop off the end of the list. That will be useful in some instances. Okay. Alright. Last thing to show is going to be sort and sorted. Okay. So when we print x, we've got Alice, Bob, Fran, John, 1, 2, 3, 4, 5. We can reverse this list by calling x.sort and then printing it. Oh, I cannot sort it. Oh, I'm sorry. I'm thinking of reverse. Okay. So this is illustrative, right? It's yelling at me. I tried to sort this list, but it didn't work. And it's saying type error. This less than operator is not supported between instances of int and string. Oh, well, I've tried to sort this list, but what Python is telling me here is I don't know how to compare numbers to strings. Doesn't work. It also doesn't know how to compare floats to strings. Okay. So it can't sort this list. Let's get rid of just, let's get rid of the numbers and put just the names. Of course, these names are already in order. Let's toss them around a bit. John, Bob, Alice, Fran. Okay. So now they're tossed around a little bit. And when I call x.sort, it takes the original order and reorganizes them. Using if they're all strings is going to do it by an alphabetical comparison. If they're all numbers, it'll do numerical order. Notably here, x.sort changed the list, right? It modified it. When I print out the old list down here, the list, it's changed. It's different. Maybe you don't want to change the list. Okay. Maybe you want a copy of the list that's been changed. The way we can do that is to call this method, which is sorted, or excuse me, this is a function, sorted. It's going to do the same thing, but it's going to return a copy of the sorted list. And it will leave the original list unmodified. Okay. So let me print for you just some original list is this print sorted copy. This original list is still unsorted. So here's my original list. I called sorted on x got the copy of it got the return value of this stored it in y printed that here it is it's sorted. But my original list is unmodified. Okay. This is a little trick that may come in handy. Sometimes you need sorted data, but you want to preserve the original list order. This is a way to do that. Okay. All right. Wow. Lot to digest here. We're going to be working exhaustively with lists in this class. So if it all doesn't sink in, don't worry about it right now. You're going to get lots of practice, but just be aware kind of of these basic methods here. Getting the length, especially this these two things, length and subscripting. Okay, we'll get lots and lots of exposure of these other things as we go on. All right, lists are a critical part of this class. Go back to your 131 notes, brush up on it. You're going to be seeing lists from day one till the very last day of the semester.