Slicing Through Sequences In Python

Vasudha Jha
Analytics Vidhya
Published in
5 min readMay 14, 2021
created using canva

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 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:

s[start:stop:step]

In the above-mentioned notation,

  1. start stands for the index in s from which we should begin slicing. The default value for start is 0.
  2. stop stands for the index in s up to which (but not including) we should slice. The default value for stop is n - 1, where n is the length of the sequence.
  3. step stands for the value by which we should increment the index when going from start to stop. The default value for step 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:

  1. The first even number in our list is 0 which is at the index 0 from the start of the list, so we can set the start index to 0
  2. The last even number in our list is 8 which is at the index 8 from the start of the list, so we can set the stop index to 9 (so that we slice up to but not including the element at the 9th index of our sequence)
  3. Every alternate number in our list from the start index onwards is an even number, so we can set our step to 2 to increment by 2 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?

This post was originally published here. If you would like to read more posts about Python Basics, you can find them here.

Thank you for reading!

--

--

Analytics Vidhya
Analytics Vidhya

Published in Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Vasudha Jha
Vasudha Jha

Written by Vasudha Jha

An engineer, artist and writer, all at the same time, I suppose.