Previous Lesson: Lesson 18: Tuples in Python

List Slices in Python provides a more advanced way of retrieving values from a list. You may see something familiar with this so it will be easy for us!

Let’s create a new file: ex16.py. Type this in:

You know that squares is just an ordinary list. But the way we access things is what we’re gonna talk about in this lesson. Just for a review, we can access or take an element from this list by indexing. On the other hand, today, we are using colons (:), to get, not an element, but to get a slice of our list.

Additionally, the rules from list slicing is still zero-based. On Line 3, the first number 2, accesses the third element in our list (which is 4); the second number 6, accesses the seventh element from our list (which is 36). The same mechanics applies with Line 4 and Line 5.

Let’s run this on our Terminal:

Nice. Did you get it? Hopefully, you did. But, notice that the seventh element (which is 36) was not printed out. That’s how slices work–the second number’s element will not get printed out. You can think of list slicing like this: [start:stop]

Remember, if there’s something in the lesson that you don’t understand, you are always free to post it on the Comments section down below. Good. You can also check out this reference to help you get your head around this topic.

A Number Omitted

To see what this means, type these down first on your code editor:

How does this work? Just a while ago, we’ve learned that the first number is the start of the slicing. But in this case, once the first number in a slice is omitted, it is taken to be the very start of the list. And then that’s up to the second number’s element. (So the result for this should be from 0 up to 36.)

If the second number is omitted (like that on Line 4), it is taken to be from that index up to the end. (So the result for this one should be from 49 up to 81.) Let’s see if I’m correct:

The third number

This may be quite a surprise for you but list slices can also have a third number, representing the step, to include only alternate values in slice. So that adds to our mnemonics: [start:stop:step]. Type this in:

Before discussing this, let’s run it first:

Okay. Line 3 says that we should start slicing from start to end. But, we would do that by skipping on the next element because we want to get the second after that element. Just like on the example, by having 2 as our step, we now have [0, 4, 16, 36, 64].

Shorcut

Negative values can be used in list slicing and in normal indexing. When negative values are used for the first and second values in a slice (or a normal index), they count from the end of the list.

So instead of counting from the second element up to the second to the last element (especially for example when we’ve got hundreds of elements in a list), we could just do this:

Let’s run it:

So that means, if we have -3 as our second number instead of just a -1, our slice would be just up to 36. Try it! Did you know that we can also reverse a list?

Reverse List!

I know. This is a very interesting feature of List Slices. All we have to do to reverse a list is to do this:

It’s that simple! Although you just need to keep this code in mind but it’s as simple as that! Let’s run this:

Cool! So if a negative value is used for the step, the slice is done backwards. I know we’ve covered a lot, but that’s all for list slices in Python! No further adieu, go experiment on your, explore, and have fun! I’ll see you on the next lesson.

Next Lesson: Lesson 20: List Comprehensions in Python

Leave a Reply

Your email address will not be published. Required fields are marked *