[MUSIC] In this video we're going to learn how to access items in a list using indexing and slicing. Now, we're combining both indexing and slicing into one video because you've actually already seen both of these concepts only with strings. Python has what it calls sequence types, and both lists and strings are sequence types. They are sequence of elements, right? A string is a sequence of characters. A list is a sequence of items. And so they behave similarly, allowing you to understand these concepts and use the same kinds of ideas across both, right? So hopefully, most of this video is completely familiar to you and nothing new. But we're going to go through it anyway, both to reinforce the concepts and so you can see how it works with list. First, let's take a look at how we can access items in a list using list indexing. But before we can do so, we actually need some list that we can access. All right, so I got my list of groceries here. Please do not judge me for I am going to buy. And now what do we do with it? Okay, so, to access the first item we have to use the 0th index. Remember, computer scientists, we start counting from zero. We are a little bit different than everybody else. Okay? And so the syntax looks pretty much exactly the same as it did when we were indexing strings. We have groceries here. Then we have an open square bracket. Then we have the numerical index, in this case zero, because I want the first item. And then a closed square bracket, okay? And if I wanted a third item, because again, I'm counting from zero, I would access groceries[2], okay? So let's see if this works the way we would expect. All right, so we get butter and bacon. Mmm, delicious, all right. And those are the correct items. I'll let you confirm that, all right? We'll can also take the length of the list, and hopefully, unsurprisingly, it's done exactly the same way as we did with strings. We use the len function. So, if we call len(groceries) we should get the number of items in the grocery list. And we get five. I'll let you count up there and confirm five is exactly the correct number of items in that list. All right. Now we're not restricted to positive indices. As we know, we can also use negative indices. So just like with strings, if I want the last element of my list I can use index [-1]. So here we have groceries [-1]. That's going to give me the last item. I can also use positive indices to get there, but I don't want to use the length, remember? Because we're counting from zero, the length is actually past the end of the list. So if I do [numitems -1] then that's the last element. And let's confirm that this all works as expected. Yes, we get asparagus both times which is the last item in the list. And just like with strings, we're not limited to [-1] to get the last item. We can use other negative numbers to get other items in the list. So if we want the third from last item, we would use the index [-3]. And that would come back and give us bacon, mm. Now we have to be careful to keep our indices within the bounds of the list. All right, so let's take a look at what happens when we do not. All right, one common error is to index with the length of the list [numitems]. All right, and as we know that should be one past the last item in the list. If we do that we get an index error. The list index is out of range. Okay, that hopefully was not surprising. Also, we have the same problem with negatives indices, right? If we use too large of a negative number that goes past the beginning of the list, we're going to be in trouble. And we get an index error again, as we hopefully would expect. All right, so just remember whether using positive or negative indices, that you need to be careful and make sure that your indices remain within the bounds of the list. And just as a reminder, this is how these indices work. If we look at this table we can see that I can index into the list using positive and negative numbers. So I can get any item, either starting from the beginning or starting from the end. As we know, we start with index 0 and move up to the length -1. So this is a six element list in my example here with the numbers 7, 8, 3, 2, 9, 4. So the positive indices go from 0 to 5. The negative indices start at -1 to give me the last item of the list and go down from there. So here we go from -1 to -6 for the six element list to get from the last to the first item. Now, let's take a look at list slicing. And again, this should be familiar from what we saw with strings. Okay. All right, so instead of using my grocery list, I'm going to create a numerical list with the range function. And, so, hopefully, you recall how this works. I'm going to do range (72, 5, -12). And that produces a list with six elements. Hopefully they make sense to you, what they are. I encourage you to play around with that range again and make sure that you understand what's going on. But basically, we started at 72, we went down by 12 each time until we got down to 5, okay? All right, so if I want to take a sublist of this, okay, I use the slice notation again. Slice notation, I have the square brackets to indicate that I am accessing elements of the list. Instead of a single index I have two indices separated by a colon. Right? So numbers [2:3] says, give me the elements in the numbers list starting at index 2, up to, but not including index 3. Similarly [1:4] means give me the elements starting at index 1 up to but not including the index 4. So I would expect one element to be in the first sublist and three in a second. And, lo and behold, that is what we get. We get the number 48, which is the correct element. That's at index 2, right, the third element. And when we go [1:4] slice we should get the second, third and forth element. And, in fact, we do. Just like with strings I can have open ended slices. Here, [1:]. All right, it says everything from index one to the end of the list, and [:3] says everything up to but not including the element at index 3 here. So the elements from index 0 to index 2. All right, and let's run that, and that is what we get. And I encourage you to look at those numbers. Make sure that those lists make sense and that they are the correct sublists that you would expect given those slices. We can also use negative indices in our slices, as we hopefully recall from the strings video. I have numbers[-2:] says, hey, give me everything from the second to last element through the end of the list. [1:-4] that slice says give me everything starting with the element at index 1 up to but not including the fourth from the last element of the list. Let's run those. And again, I encourage you to think about those slices and make sure that what got printed out here matches your expectation and makes sense to you. We can also have empty slices. Remember that with slicing, when you have an empty slice, instead of being an error it just gives you an empty item, or an empty list, or an empty sequence, okay? So, both of these [3:2] at index 3 up to but not including the element at index 2. Well, there's nothing in that range, so hopefully we will get an empty list. Similarly [10:12], those numbers are past the end of the list, so I would expect there to be nothing in that list as well. And if as we see here in the output, we get two empty lists from those slices. Okay, hopefully, this all makes sense. Hopefully, this matches your expectation. In Python, both lists and strings are sequence types. Now, hopefully, you're starting to appreciate some of the niceties of Python. Because both of these types are sequence types, the way that indexing and slicing works is exactly the same. So if you understand how to index into strings and slice strings, then you also understand how to index and slice lists. So hopefully this video here was just a review for you of the string indexing and string slicing video, and by seeing it again, it helped to reinforce those concepts for you.