List Slicing in Python

Ridwan Yusuf
5 min readJan 28, 2023

--

Slicing can be used to access ranges of items in a list. This guide describes techniques that can be used to access/retrieve ranges of items while working with Python list.

Here goes the syntax:

new_list = original_list[start: end : step]

The ‘start’ parameter indicates the index at which the new list will begin, while the ‘end’ parameter specifies the upper bound index. It is important to note that the slicing operation does not include the index of the upper bound, but instead stops at the index before it. The ‘step’ parameter (which defaults to 1 if not provided) determines the interval at which the next item is selected. A step of 1 means the immediate following item, 2 means every 2nd item, and so on.

Let’s take a look at this example:
[*]Snip

initial_list = ['A','B','C','D','E','F']
new_list = initial_list[0:4]
print(new_list)

[*]Output
[‘A’, ‘B’, ‘C’, ‘D’]

We created a new list using this: initial_list[0:4]. This means that the new list would start from index zero and go up to index 4 minus 1(i.e. 3). The step is omitted, so it uses the default value of 1.

Let’s see another example:
[*]snip

initial_list = [1,2,3,4,5,6,7,8,9]
new_list = initial_list[1:6:2]
print(new_list)

[*]Output
[2, 4, 6]

initial_list[1:6:2]:
This slicing of list initial_list[1:6:2] will select elements starting at index 1, up to but not including index 6, and only taking every second element (indicated by the step of 2). It will create a new list containing items at index 1, 3 and 5 from the original list.
What if the upper bound is not giving?
We have seen some cases where the steps were missing and the default value of 1 is used. If no upper bound is mentioned then the slicing moves up until the end of the list. The syntax may look like this: initial_list[1::]
So the starting index is 1, the step is not given which falls back to using default value of one. Since the upper bound or end is not provided, the slicing moves to until the last item in the original list. [1::] is therefore technically evaluates to [1::1]

[*]Snip

initial_list = [1,2,3,4,5,6,7,8,9]
new_list = initial_list[1::]
print(new_list)

[*]Output
[2, 3, 4, 5, 6, 7, 8, 9]

[1::]: Here the starting index is 1(technically the 2nd item in the list), since no upper bound or end is provided it moves till the end of the list. Also, the step is not given so it uses the default value of 1.

Let’s change the step to a value of 2.
[*]Snip

initial_list = [1,2,3,4,5,6,7,8,9]
new_list = initial_list[1::2]
print(new_list)

[*]Output
[2, 4, 6, 8]

[1::2] will select elements starting at index 1, to the end of the list, but only taking every second element (indicated by the step of 2). It will create a new list containing items at index 1, 3, 5, … from the original list to the end of the list.

What if the starting point is not provided?
The new list starts from the beginning if the starting index is not giving. The syntax would then look like this-> [::1]
This means it should start from the 1st item in the list(since no start in provided), move to the last item(since no end is provided) and take a step of 1. Technically, this syntax is the same as [::]. You got it, right? Recall that the default step is 1 if not given.

[*]Snip

initial_list = [1,2,3,4,5,6,7,8,9]
new_list = initial_list[::1]
print(new_list)

[*]Output
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Negative Index.

Most of the examples we used earlier use +ve indexes. Let’s look at examples with negative index. Essentially the same concepts explained above apply here also. The main difference is that with negative indexes, the list is indexed in reverse order, starting from -1 for the last element, -2 for the second-to-last element, and so on.

Quick Note: -ve index in general starts from the last item in the original list. For instance, to get the last item from a list we simply use the index of -1, -2 for the item before the last item and so on.

[*]snip

my_list = [1,2,3,4,5,6]
print(my_list[-1])
print(my_list[-2])
print(my_list[-8])

[*]Output
6
5
IndexError: list index out of range

An attempt to get index of -8 throws IndexError(Out of range) since such index is no where to be found in the list.

Selecting the last two elements in the list:
[*]snip

my_list = [1,2,3,4,5,6]
print(my_list[-2:])

[*]Output
[5, 6]

[::-1] ?
Quess what this means?

The start and end indices are not specified, so it defaults to the start and end of the list. The step is -1, which means it will go through the list in reverse order. Just note that any -ve step would start from a backward/reverse order
The result of this is just a reverse version of the original list.
[*]snip

initial_list = [1,2,3,4,5,6,7,8,9]
new_list = initial_list[::-1]
print(new_list)

[*]Output
[9, 8, 7, 6, 5, 4, 3, 2, 1]

This reversed the original list. Python provides a built-in function reverse() to reverse a list.

Reverse can be more efficient that slicing to reverse a list especially for small lists. The reverse() function modifies the original list while slicing creates a new list. So it depends on the use case, it you want to keep the original list, using slicing to create a new list comes in handy.

Let’s take a look at this
[*]snip

initial_list = [1,2,3,4,5,6,7,8,9]
new_list = initial_list[-1:-5:-1]
print(new_list)

[*]Output
[9, 8, 7, 6]
This starts from the last item(-1) taking a step of (-1) until index -5 which is the upper bound

[*]snip

new_list = initial_list[-2:-5:-1]
print(new_list)

[*]Output
[8, 7, 6]

Items in a list can be cleared using the using the built-in clear() method. This works for dictionary also.
[*]Snip

my_list = [1,2,3,4,5,6]
my_list.clear()
print(my_list)

[*]Output
[]

List slicing is a powerful tool for manipulating lists in Python, and I’m glad that this guide has helped make it clearer. Indeed, you can achieve a lot of different manipulation using slicing. For example, to select every second element of a list you can use the syntax my_list[::2].

Thanks for reading and feel free to explore my video collection on YouTube for more educational content. Don’t forget to subscribe to the channel to stay updated with future releases.

--

--

Ridwan Yusuf
Ridwan Yusuf

Written by Ridwan Yusuf

RidwanRay.com -I provide engineers with actionable tips and guides for immediate use

No responses yet