Slicing Through Sequences In Python
One of the easiest ways to extract a subset of elements from an iterable in Python is by applying the concept of slicing. In this post, let’s understand how we can slice through any iterable, i.e, strings, lists or tuples in order to carve out a subset!
Before we begin, let’s refresh our understanding of how indexing works in Python.
Let’s declare a list which contains all the whole numbers from
num_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Since Python follows zero based indexing, to access the first element of our list, we use the
index operator () by passing in the index
10 elements in our list, ranging from index
9 and if we try to print the element at the 10th index using
num_list, we get the following error:
IndexError: list index out of range
Python also allows elements in a sequence to be accessed using negative indices by counting from the end of the list.
num_list[-1] accesses the first element from the end of a list, which is
This means that:
num_list == num_list[-10] # or 0 in the list num_list == num_list[-9] # or 1 in the list num_list == num_list[-8] # or 2 in the list num_list == num_list[-7] # or 3 in the list num_list == num_list[-6] # or 4 in the list num_list == num_list[-5] # or 5 in the list num_list == num_list[-4] # or 6 in the list num_list == num_list[-3] # or 7 in the list num_list == num_list[-2] # or 8 in the list num_list == num_list[-1] # or 9 in the list
Hence, for a sequence
s of length
ith element of
s can be accessed as,
s[i] or s[i - n]
Now that we have refreshed our understanding of how sequences are indexed in Python, we can extend this knowledge to slice out subsequences from a sequence
s using the following notation:
In the above mentioned notation,
startstands for the index in
sfrom which we should begin slicing. The default value for
stopstands for the index in
supto which (but not including) we should slice. The default value for
n - 1, where
nis the length of the sequence.
stepstands for the value by which we should increment the index when going from
stop. The default value for
If we perform a slice operation on our
num_list, which contains
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], without providing values for
step, then the slice operation will use default values of
1 respectively for each of the three variables.
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
On first glance, it seems that
num_list was printed. But we need to remember that the slice operation always returns a new list. In this case, since the values for
step for our subsequence are the same as the original sequence (since we are using the default values), we get a new list containing the same elements as our original list.
Next, let’s see the different ways in which we can extract a subsequence containing all the even numbers from
num_list in ascending order.
1. Providing Start, Stop and Step
In order to do so, let’s determine our
- The first even number in our list is
0which is at index
0from the start of the list, so we can set the
- The last even number in our list is
8which is at index
8from the start of the list, so we can set the
9(so that we slice upto but not including the element at the
9th index of our sequence)
- Every alternate number in our list from the start index onwards is an even number, so we can set our
2to increment by
2in each step.
A list of even numbers in ascending order from
0-9 is printed out as the output when we run this code:
[0, 2, 4, 6, 8]
2. Providing Start and Step
Since the default value of the
stop index for our subsequence is already
len(num_list) - 1), we can get the same even number list as above without providing a
stop index to our slice like so:
3. Providing Stop and Step
Similarly, since the default value of the
start index for our subsequence is already
0, we can get the same even number list without providing the
start index to the slice like so:
4. Providing Only Step
Finally, since the default values of the
stop indices for our subsequence are the same as provided in the first way, we can get our even number list by just setting the
step variable to
We can even use negative indices to get the same result like so:
# 1: print(num_list[0:-1:2]) # setting stop index to -1 to access the first element from the end # or print(num_list[:-1:2]) # using the default value for start index # 2: print(num_list[-10:9:2]) # setting start index to -1 to access the last element from the end # or print(num_list[-10::2]) # using the default value for stop index # 3: print(num_list[-10:-1:2]) # using negative start and stop indices
In the above mentioned examples, we have always provided a value for
step since it helped us increment by two steps from the start index to the end in order to find all the even numbers.
In case we only need to extract the first three numbers from the start of the
num_list, we don’t need to pass a value for
step since we will be incrementing by
1, which is the default
[0, 1, 2]
A Common Application:
A very common application of slicing is reversing a sequence.
For example, if we want to reverse
num_list, we can do so by setting the
step variable to -1:
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
When we set the
-1, Python internally swaps the
stop indices to give us a reversed list.
So, if you are asked to reverse the string
"hello" without using Python’s built-in
reverse method, you can use slices to do so by running the following line of code:
Finally, a Question For You:
Can you come up with different ways of extracting a list of odd numbers, sorted in descending order, from the
If you would like to read more posts about Python Basics, you can find them here.
Thank you for reading!
Copyright 2021-present Vasudha Jha.
Released under the Creative Commons Attribution-ShareAlike 4.0 International License.