To get a sublist in Python, you can use list slicing.
In this example,
my_list
with some elements.my_list
. Slicing is done using the syntax [start:end]
, where start
is the starting index (inclusive) and end
is the ending index (exclusive).my_list = [1, 2, 3, 4, 5]
sub_list = my_list[1:4]
print(f'Sublist: {sub_list}')
Sublist: [2, 3, 4]
In this example,
my_list
with some elements.my_list
by specifying different start and end indices.my_list = [10, 20, 30, 40, 50, 60]
sub_list = my_list[2:5]
print(f'Sublist: {sub_list}')
Sublist: [30, 40, 50]
In this tutorial, we learned How to get Sub List in Python language with well detailed examples.