Introduction
Slicing is a powerful technique in Python that allows you to extract specific portions of a list with ease. With slicing, you can effortlessly retrieve a subset of elements from a list based on their indices or create new lists by extracting desired segments. This essential feature enables you to manipulate data efficiently, whether you need to access a range of values, reverse a list, or extract every nth element. In this tutorial, we will explore the syntax and various techniques of list slicing, empowering you to manipulate and extract data from lists like a pro. Let's get started and unlock the full potential of list slicing in Python!
Table of Contents :
- Slicing a list in Python
- List Slicing Operations in Python
- get the first n-elements of a list in Python
- get the last n-elements of a list in Python
- get every nth element of the list in Python
- reverse a list in Python using slicing
- substitute a part of the list in Python
- resize the list in Python
- delete list element using slicing in Python
Slicing a list in Python :
- Slicing is a process of getting a sub list (a portion of the list) from the original list.
- Slicing process in Python can be used for many purposes :
- getting a sub-list from main list
- updating the main list
- resizing the list
- deleting a portion of the list.
- The syntax of slicing a list in python is :
sub_list = original_list[begin: end: step:]
- The sub_list is extracted based on the values of
begin
,end
andstep
arguments passed in the square brackets.- The list is sliced from the
begin
index up-to theend
index. - The list is sliced in steps of the
step
argument. - All these three arguments are optional.
- The list is sliced from the
- If we omit any or all of these arguments, their default values are used for slicing the list :
- The default value of
begin
argument is 0. - The default value of
end
argument is the length of the list. - The default value of
step
argument is 1.
- The default value of
- All these three arguments
begin
,end
andstep
can take positive as well as negative values.- Positive arguments slice the list from the first element to the last element.
- Negative arguments slice the list from the last element to the first element.
- Code Sample :
my_list = [1, 2, 3, 4, 5, 6, 7, 8]
print("my_list = ", my_list)
# Slicing Using values of begin end and steps
sub_list = my_list[2:6:2]
print("sub_list = ", sub_list)
# Output
# my_list = [1, 2, 3, 4, 5, 6, 7, 8]
# sub_list = [3, 5]
# Slicing Using default values of end and steps
sub_list = my_list[2:]
print("my_list = ", my_list)
print("sub_list = ", sub_list)
# Output
# my_list = [1, 2, 3, 4, 5, 6, 7, 8]
# sub_list = [3, 4, 5, 6, 7, 8]
# Slicing Using default values of begin and steps
sub_list = my_list[:4:]
print("my_list = ", my_list)
print("sub_list = ", sub_list)
# Output
# my_list = [1, 2, 3, 4, 5, 6, 7, 8]
# sub_list = [1, 2, 3, 4]
# Slicing Using negative value of begin
sub_list = my_list[-3:]
print("my_list = ", my_list)
print("sub_list = ", sub_list)
# Output
# my_list = [1, 2, 3, 4, 5, 6, 7, 8]
# ub_list = [6, 7, 8]
List Slicing Operations in Python :
There are a lot of operations that can be performed using list slicing in Python :
- get the first n-elements of a list in Python
- get the last n-elements of a list in Python
- get every nth element of the list in Python
- reverse a list in Python using slicing
- substitute a part of the list in Python
- resize the list in Python
- delete list element using slicing in Python
How to get the first n-elements of a list in Python :
- The begin argument is omitted to get the first n elements of a list.
- The basic syntax of extracting the first n elements of a list in python is :
sub_list = original_list[:n]
- We can explicitly write the begin argument as 0 i.e. we can write :
sub_list = original_list[0:n]
- but in practice it is not needed as default value of begin argument is itself 0.
- Code Sample :
my_list = [1, 2, 3, 4, 5, 6, 7, 8]
print("my_list = ", my_list)
n = 5
sub_list = my_list[:n]
print("sub_list = ", sub_list)
# Output
# my_list = [1, 2, 3, 4, 5, 6, 7, 8]
# sub_list = [1, 2, 3, 4, 5]
How to get the last n-elements of a list in Python :
- We use negative indexes to get the last n elements of the list.
- The end argument is omitted to get the last n elements of a list.
- The basic syntax of extracting the last n elements of a list in python is :
sub_list = original_list[-n:]
- Code Sample :
my_list = [1, 2, 3, 4, 5, 6, 7, 8]
print("my_list = ", my_list)
n = 5
sub_list = my_list[-n:]
print("sub_list = ", sub_list)
# Output
# my_list = [1, 2, 3, 4, 5, 6, 7, 8]
# sub_list = [4, 5, 6, 7, 8]
How to get every nth element of the list in Python
- The step argument is used to get every nth element of the list.
- The begin and end arguments are optional.
- The syntax of getting every nth element is :
sub_list = original_list[begin: end: n]
- If we omit the begin and end arguments their default values will be used.
- The syntax in that case will be :
sub_list = original_list[::n]
- Code Sample :
my_list = [1, 2, 3, 4, 5, 6, 7, 8]
print("my_list = ", my_list)
n = 3
sub_list = my_list[::n]
print("sub_list = ", sub_list)
# Output
# my_list = [1, 2, 3, 4, 5, 6, 7, 8]
# sub_list = [1, 4, 7]
How to reverse a list in Python using slicing :
- We can use negative step argument to reverse a list in Python.
- The syntax of reversing the list is :
reversed_list = original_list[::-1]
- When we give negative step argument, sub-list is created by including elements starting from the last to the first, hence the list gets reversed.
- Code Sample :
my_list = [1, 2, 3, 4, 5, 6, 7, 8]
print("my_list = ", my_list)
rev_list = my_list[::-1]
print("rev_list = ", rev_list)
# Output
# my_list = [1, 2, 3, 4, 5, 6, 7, 8]
# rev_list = [8, 7, 6, 5, 4, 3, 2, 1]
How to substitute a part of the list in Python :
- The slicing can be used to change the values of items in the list.
- The syntax of modifying values in list is as follows :
original_list[begin: end] = ["new_item_1", "new_item_2"]
- Code Sample :
my_list = [1, 2, 3, 4, 5, 6, 7, 8]
print("my_list = ", my_list)
my_list[2:5] = [300, 400, 500]
print("my_list = ", my_list)
# Output
# my_list = [1, 2, 3, 4, 5, 6, 7, 8]
# my_list = [1, 2, 300, 400, 500, 6, 7, 8]
How to resize the list in Python :
- The slicing can be used to resize the list in python
- The syntax of resizing the list is as follows :
original_list[begin: end] = ["new_item_1", "new_item_2"]
- Code Sample :
my_list = [1, 2, 3, 4, 5, 6, 7, 8]
print("my_list = ", my_list)
my_list[2:5] = [300, 400, 500, 550, 560]
print("my_list = ", my_list)
# Output
# my_list = [1, 2, 3, 4, 5, 6, 7, 8]
# my_list = [1, 2, 300, 400, 500, 550, 560, 6, 7, 8]
How to delete list element using slicing in Python :
- We can use the
del
keyword along with list slicing to delete a portion of the original list. - The syntax of the deleting elements is as follows :
del original_list[begin: end]
- Code Sample :
my_list = [1, 2, 3, 4, 5, 6, 7, 8]
print("my_list = ", my_list)
del my_list[2:5]
print("my_list = ", my_list)
# Output
# my_list = [1, 2, 3, 4, 5, 6, 7, 8]
# my_list = [1, 2, 6, 7, 8]
Prev. Tutorial : Sorting a list
Next Tutorial : Unpacking a list