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 0
to 9
.
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 0
.
print(num_list[0])
Output:
0
There are 10
elements in our list, ranging from index 0
to 9
and if we try to print the element at the 10th index using num_list[10]
, 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.
For example, num_list[-1]
accesses the first element from the end of a list, which is 9
.
This means that:
num_list[0] == num_list[-10] # or 0 in the listnum_list[1] == num_list[-9] # or 1 in the listnum_list[2] == num_list[-8] # or 2 in the listnum_list[3] == num_list[-7] # or 3 in the listnum_list[4] == num_list[-6] # or 4 in the listnum_list[5] == num_list[-5] # or 5 in the listnum_list[6] == num_list[-4] # or 6 in the listnum_list[7] == num_list[-3] # or 7 in the listnum_list[8] == num_list[-2] # or 8 in the listnum_list[9] == num_list[-1] # or 9 in the list
Hence, for a sequence s
of length n
, the i
th 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:
s[start:stop:step]
In the above-mentioned notation,
start
stands for the index ins
from which we should begin slicing. The default value forstart
is0
.stop
stands for the index ins
up to which (but not including) we should slice. The default value forstop
isn - 1
, wheren
is the length of the sequence.step
stands for the value by which we should increment the index when going fromstart
tostop
. The default value forstep
is 1.
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 start
, stop
and step
, then the slice operation will use default values of 0
, 9
and 1
respectively for each of the three variables.
print(num_list[::])
Output:
[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 start
, stop
and 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 start
, stop
and step
variables:
- The first even number in our list is
0
which is at the index0
from the start of the list, so we can set thestart
index to0
- The last even number in our list is
8
which is at the index8
from the start of the list, so we can set thestop
index to9
(so that we slice up to but not including the element at the9
th index of our sequence) - Every alternate number in our list from the start index onwards is an even number, so we can set our
step
to2
to increment by2
in each step.
print(num_list[0:9:2])
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 9
(len(num_list) - 1
), we can get the same even number list as above without providing a stop
index to our slice like so:
print(num_list[0::2])
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:
print(num_list[:9:2])
4. Providing Only Step
Finally, since the default values of the start
and 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 2
:
print(num_list[::2])
Other ways:
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
Sidebar:
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 step
value.
print(num_list[0:3])
Output:
[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:
print(num_list[::-1])
Output:
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
When we set the step
to -1
, Python internally swaps the start
and 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:
print("hello"[::-1])
Output:
"olleh"
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 num_list
?