Locked learning resources

You must own this product to watch this lesson.

Locked learning resources

You must own this product to watch this lesson.

Indexing and Slicing

This lesson is from the Real Python video course by Christopher Bailey.

00:00 In this video, you’ll practice list indexing and slicing. The elements of a list can be accessed by an index. To do that, you name the list, and then inside of a pair of square brackets you use an index number, like what I’m showing right here.

00:17 That allows access to individual elements within the list. The indexing for the list is zero-based. So if you have a list such as this, with these six elements, the indices start with 0 and would go up to 5.

00:36 Let me have you try that out. Start with a new list.

00:47 Here’s a. As described, indexes are zero-based for lists, so a[0] would access the first item in that list.

01:03 a[2] would access the third one. And in this case, a[5] would access the last. Another way to get there would be a and the len() (length) of a minus 1.

01:18 If you use an index value that’s too high, Python will raise an exception—an IndexError saying that the list index is out of range. Negative indexing is available also.

01:33 If you want to access the last item, you’d start with -1. So continuing to work with that list a I’ll have you try out negative indexing.

01:44 a[-1] will access the last item, a[-2], and so forth. So in this case -6—which is also negative the length of your list—would return the first item.

02:05 If you try to access an index that’s beyond the scope, you’ll get that index out of range also. Slicing is available too. Slicing’s an indexing syntax that’s going to extract a portion from your list. So in this example, if a is your list, then inside your square brackets you would have your two index numbers separated by a colon (:), and it’s going to return a portion of that a list that will start with position m and go up to but not include index n.

02:38 Using that same example from before, if you took a[2:5], you’d get the three objects in the middle, starting at index 2 and going up to index 4, but not including 5.

02:50 Let me have you try it out. Okay, so here’s your list. What if you wanted to slice? Let me have you start at index 2 and then using a colon. For the second index, use 5. From 2 up to but not including 5.

03:05 Great! You can also use negative indexes, so you could say starting at -5 and going to -2, which is the same as going from 1 to 4.

03:19 And you can confirm that here

03:25 as being True.

03:29 A shorthand is available. By omitting the first index, your slice is going to start at the beginning of the list and go up to the second index. If you omit the last index, it’s going to extend the slice from the first index and go all the way to the end of the list. And if you were to omit both indexes, it’s going to return a copy of the entire list.

03:54 And unlike with a string, it’s a copy—not a reference to the same object. So, what if you were to omit the first index? In that case, it will start at the beginning and go up to but not include that index. The same as from 0 to 4.

04:12 And in the same way, removing the second index will go all the way to the end, starting with this index up to the end. Here, you can use len(), which will return a value of 6, and if we go from 2 up to but not include 6, as the index.

04:35 If you were to remove both indexes and just have a [:], it returns the entire list. You can try this out by seeing if it is equal to a, which is True. But this is a copy and not a reference, whereas if you tried a is a[:]—it’s a copy.

05:01 It’s not a reference to the original. That could be confusing because if you’ve worked with strings before, if you have a string, s, which is 'mybacon', using the syntax of just the [:] does return the entire string.

05:20 What’s interesting about it is not only is s == s with the colon-only index, but it also is a reference to that object. Whereas with a list, it returns an entirely new object.

05:37 It’s possible to add a third index after the additional colon. That third index indicates a stride, but it’s also sometimes called a step. So in this example, if you had a slice that went from 0 to 6, with a step of 2, you would return the objects at index 0, 2, and 4'spam', 'bacon', and 'ham'.

06:02 So if you went from index 0 up to 6 with a stride of 2, you’ll see it grab the first, third, and the fifth items, skipping over with a stride of two.

06:14 If you started that at 1 instead, you’d get the other three.

06:20 And it is possible to have a negative stride, though you’d put the first index in as the highest value, and then where you want to go up to, a 0, and in this case it’s going to go to -2.

06:36 In fact, a simple syntax for just simply reversing your list is to leave the first two indexes blank with a colon, and then the second colon and a -1—that will reverse your list.

06:50 Next, you’re going to practice using operators and some of Python’s built-in functions on your list.

You must own this product to join the conversation.