Slicing in Python
Coding with Python
🕑 This lesson will take about 20 minutes
Extracting (slicing) a range of elements from a list
In the last lesson, we looked at sequences, and more specifically lists. Sequences such as lists can be sliced. What “slicing” means is to extract certain parts of a sequence. Slicing works on both lists and strings, but first we will look at examples of slicing lists. To show you how slicing wokrs, let’s first create a new list we can use as an example in this lesson. We will just make a list called numbers and fill it up with some…numbers!
numbers = [0,1,2,3,4,5,6,7,8,9]
So now we have a list of numbers. We will extract some numbers from this list. If we write numbers[3:7] it will extract the elements of the list starting from index 3 and ending with the last element before index 7. So we will be extracting the numbers 3, 4, 5, and 6 from the list. If we wanted to also extract 7 then we would have to specify numbers[3:8] instead because Python will grab everything from the first index you specify up to whatever comes before the second index you specify (after the colon : character).
Slicing the left or right portion of a list
So what happens if you want to extract the numbers 6, 7, 8, and the last number, 9? If you wrote numbers[6:9] it would only extract the numbers 6, 7, and 8. You could write numbers[6:10] and this would work even though there is no element with an index of 10 in the list (because Python will extract the elements from index 6 up to the element before index 10).
There is a neater solution though. You can just specify the starting index and specify no index after the colon. In other words, if you write numbers[6:] it will return the numbers starting from index 6 to the very last number in the list which is 9. You could also use the negative index numbers and count backwards, for example numbers[-4:] would extract the same elements as numbers[6:] . Check out the example below.
If you are slicing from the beginning of a list, you can also leave the first number out. For example, if you want to extract elements from the indexes 0, 1, 2, and 3 in the example list above, then you could just write numbers[:4] instead of numbers[0:4]. If you want to extract the entire list, you could write numbers[:] (or just numbers).
There is an optional third parameter you can use when slicing a sequence. When you perform a slicing operation on a list using two parameters in the square brackets, for example, numbers[3:8], you will extract, for example, the numbers 3, 4, 5, 6, and 7. In this example, the number 3 is the first parameter and the number 8 is the second parameter (after the colon).
Slicing with increments
Adding a third value after another colon inside the square brackets tells Python how many elements you want to increment by. For example, numbers[3:8:2] would tell Python to extract elements from the numbers list starting from index 3 up the last element before index 7, but to increment by 2 (in other words, extract every second element). Python would only extract numbers 3, 5, and 7 (every second element) from the example list. Writing numbers[3:8:3] would tell Python to increment by 3, and so on. Check out the example below and try it out for yourself.
Slicing a list in reverse order
You can even slice a list in reverse order (from right to left) by specifying the index range and a decrement (how much to count down by). For example, if you wanted to extract the elements from the 9th index of the example list down to the last element after the 4th index (in other words, the element in the 5th index position), then you would write numbers[9:4:-1]. If you want to do the same, but extract every second element in that range in reverse order, then you’d write numbers[9:4:-2].
If you wish to extract the entire list in reverse order, you would write numbers[::-1], and if you wanted to extract every second element from the entire list in reverse order, you would write numbers[::-2].
Slicing strings
Remember, all of these slicing operations we have performed on the example list also work on strings - strings are sequences (a sequence of characters) just like lists are a sequence of elements, so we can use the same slicing methods to extract parts of a string just like we would to extract elements from a list. Check out the example below.
That’s enough slicing for this lesson, but we will check out some more slicing methods we can use to add, remove, or replace elements in a list in the next lesson.
Next lesson: Editing lists