Slicing Through Sequences In Python

May 14 · 5 min read

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,

1. `start` stands for the index in `s` from which we should begin slicing. The default value for `start` is `0`.

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`
`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# orprint(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`?

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Analytics Vidhya

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

Written by

Vasudha Jha

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

Analytics Vidhya

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

Implementing an algorithm by modelling an FSM in an HDL

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app