 Hi everybody, so we're going to dig into a data structure now your very first data structure, which is the array list Now the good news is you already know how to work with lists. You know what they're capable of So we're going to talk about how they work in this video, right? So I'm looking at your array list Handouts sheet right here. I'm going to be working on the board because It's easier for me that way and I got a little clip of it in the bottom Right that you can kind of follow along at least for a little while so The way the array works remember that the key point here is that when you make a list in Python It allocates which is the fancy term for setting aside a space in memory a fixed region It's trying to get my mouse to kind of show up over here. It's a little tricky to see It shows a fit it chooses a fixed region in memory and that region is contiguous, right? It's all packed in together. So all the data related to a list in Python is in one area of Memory, okay? It'll be a different area every time you run the program But it's all together all the list information is right there together, okay? So the beginning of the list is What we call the object head now you don't need to worry about what's in a Python object header It's something Python uses for housekeeping and keeping track of stuff But what's important is that it is a constant fixed size for your computer So every list you ever make this object header is going to be say 24 bytes It may be a little different on your computer, but it's going to be the same It's followed by the length Stored as an integer okay the length of the list and then each object reference takes eight bytes Okay, and that's again those numbers may be a little different the specific numbers may be different for your computer But they will always be constant for every Python list you make on your computer Now Python uses that fact to its advantage these constant size things as well some pretty simple arithmetic To really do some cool stuff with this array of memory, okay? So the first operation we'll talk about is This one here indexing All right, so whenever you access a list element by its index number. That's called index Okay, all indexing operations in Python are they go of one For the list data type and that's because of this array list data structure that the list data type uses Okay, so how does it work? This is kind of cool How does it work? Well How could we find in a block of memory? The say item at index two Okay, oh it's math actually Let's start by asking ourselves the question. Where's the item at index zero? Where's going to be the first item in the list? Let's look at this little map down here If we start let's take the starting point of the object in memory Now Python gives you a method that will tell you where an object is in memory. Do you remember what it is? It is the ID function So if I call ID of list Right, that's going to give us a memory address that points to the beginning here, right points to the beginning Okay, so ID gives me back Let's say, you know for simplicity sake. I put this at index at address zero could be something else It's totally fine. It could be five million. It could be seven hundred twenty four thousand whatever it is, right? It's a number. That's the address in memory where this list begins Well, I want to get let's start with I want to focus on the item at index zero Okay, well, how can I get there? Recall that I know the starting in that the starting point in memory And then I also know that this object header has a fixed size always 24 bytes, okay So if I start at zero And then I add 24 to it Where would that take me in memory? Right. Well, it would take me past the object header down here. It's where the length is kept Okay, well, I'm not interested in the length right now. Let me skip over this and I said here that the length is also stored as a fixed size. It's going to be eight bytes in the case of my So if I add eight to it Right add all these things up That what I'm saying is that list Sub-zero is At this location Address 32 in memory and I got that through simple arithmetic Right. I'm just at I start at the beginning of my list given by ID and then I add these fixed sizes, too Okay, so Python can very quickly say all right. Let me just do some real quick math here and Oh, okay. The thing I want is that memory address 32. Give me that And we talked about in the previous video how reading and writing things from to and from memory That's something that happens very fast. It's constant time. It's big of one time to read from memory Okay arithmetic also It's big of one time addition computers are super fast at math. They love math. They can do it super fast So all of your arithmetic operators are they go with one time as well So even though there's a couple of steps here, you know when we're talking order of magnitude We don't care about that. This is like three or four or five steps of computation We just care that the order of magnitude is constant time. Okay So this gives us the index zero, right? What if we want an arbitrary index? What if we want index two? Well for that you need to know how big each reference is in memory and fortunately you do You know that every item that is stored in a Python list Is stored as a memory reference of the actual value but saying hey Alice is the first item she lives over here, but this is her memory address Her address takes eight bytes. It is a fixed size, right? Again, it may be a little different for your particular computer, but it will always be the same So if we know that she each one of these references takes eight bytes We can use that to our advantage If we want index zero, how far down do we skip from this point? Now what if we want to go to index one? this guy Well if this guy is size eight This line is eight bytes. I multiply it by the index One if I want to go down two slots. I multiply it by eight by the index two Okay, so what I can do then is I can make a more generic formula that says We go from the idea of list Plus 24 for the object header plus eight for the length of the list plus eight times The index whatever that index is here. It's two Okay, and that gives you the location in memory So by using math Python is very or quick able to very quickly Compute where something is in an array based list. That's the power of the array Okay, now it only works though whenever you have a contiguous block Okay, if this was like Empty here for somehow right and then this was actually index zero This wouldn't work. The math would not work out. This only works because the array is contiguous Okay, let's get everybody back