List Slicing
List slicing refers to accessing a sub portion of a list using a colon-separated indexing technique.
Syntax
my_list[start:end]
The statement my_list[0:2]
will return the first two elements from the list, at index 0 and 1, respectively.
my_list = [1, 5, 10, 15, 20, 25, 30]print(my_list[0:2])
List slicing can also be utilized when updating lists. For example, we can update the first two values of my list to 50
and 55
, respectively, using the code snippet below.
my_list = [1, 5, 10, 15, 20, 25, 30]my_list[0:2] = 50, 55print(my_list)
Inclusive and exclusive indices
It’s important to understand that the specified sub-portion is inclusive of the element at the starting index and exclusive of the element at the ending index. In other words, we can say that the element at the starting index is part of the result, while the element at the ending index is not.
The step parameter
The step parameter is an optional parameter that can be used in list slicing to skip elements.
my_list[start:end:step]
For instance, my_list[1:5:2]
will return a list containing elements at indices 1, 3, and 5, skipping every second element.
my_list = [1, 5, 10, 15, 20, 25, 30]print(my_list[1:5:2])
Slicing from the beginning
The syntax my_list[:end]
retrieves elements from the beginning of the list up to, but not including, the element at index end.
my_list = [1, 5, 10, 15, 20, 25, 30]slice_list_from_start = my_list[:4]print(slice_list_from_start)
The above code will result in a list including elements from the start of the list up to the 4th index ([1,5,10,15])
. Note how 20
(the 4th index) is not included in the list.
Slicing from a specific index till the end
The syntax my_list[start:]
retrieves elements from the start index, including the element at the start, until the end of the list.
my_list = [1, 5, 10, 15, 20, 25, 30]slice_list_till_end = my_list[4:]print(slice_list_till_end)
The above code will result in a list including elements from the 4th index to the end of the list ([20,25, 30])
.
Negative slicing
In Python, we can also use negative indices for slicing, which allows us to access elements from the end of the list. Negative indices count from the end of the list, starting with -1 for the last element.
Consider a scenario where the last three elements of a list are required, but the size of the list is unknown. We can use negative slicing to obtain the last three values.
my_list = [1, 5, 10, 15, 20, 25, 30]last_three_elements = my_list[-3:]print("Last three elements:", last_three_elements)
Let’s take a look at a code that’s used to skip every second element from the end using this method.
my_list = [1, 5, 10, 15, 20, 25, 30]skip_every_second_from_end = my_list[-1:-6:-2]print(skip_every_second_from_end)
We can even use negative slicing to reverse a list! The following code reverses my_list
effectively. This is done by leaving the start and end indices empty, and specifying step as -1
, which instructs the list to begin backward and move in steps of one.
my_list = [1, 5, 10, 15, 20, 25, 30]reversed_list = my_list[::-1]print(reversed_list)