Signup/Sign In

How to Slice Python List

In this tutorial, we will learn how to perform a slice on the list. In a python programming language, if we want to access elements from the sequence, we can access them through the index method. The positive index starts from 0. The negative index starts from -1. But what if we want to access several elements or range from the list. Let us say from element 1 to 5. We do not want to access those elements one by one by index and again append them into another list. In that situation, the slice concept comes into the picture. Slice is a way to extract elements from the list. The below shows the syntax of the slice.

Syntax:

list1[start:stop:step] 

Start: it indicates the index where the slice has to start. The Default value is 0.

Stop: It indicates the index where the slice has to end. The default value is the max allowed index of the list. It is the length of the list.

Step: It indicates the increment value. The default value is 1.

Example: Slice with positive index.

In the below example we initialize the list_1 with elements from 1 to 10. Next, using the slice operator we are trying to extract elements. We give the start value 0, and stop value 5 and step value 1. When we run the program, we will get elements from 1 to 5 as the end value is non-inclusive.

#Intializing list
#slicing the list using slice operator
list_1=[1,2,3,4,5,6,7,8,9,10]
print("The list is:",list_1)
list_2=list_1[0:5:1]
print("The sliced list is:",list_2)

Once we run the program we will get the following output.


The list is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The sliced list is: [1, 2, 3, 4, 5]

Example: Another example of Slice with positive index.

In this example let start extract elements from the list by giving different values to start, end, and stop.

#Intializing list
list_1=[1,2,3,4,5,6,7,8,9,10]
print("The original list is:",list_1)
print("The sliced list are:")
print(list_1[2:6:2])
print(list_1[4:1000])
print(list_1[2::2])
print(list_1[::])
print(list_1[:25:])
print(list_1[25::])

In the above example, we tried to extract elements by giving different values to start, end, and step.

In code line 6, even when we gave the end value out of range, we will not get any error. It will print elements from index 4 to the end index of the list with step 1. (Default value is 1)

In code line 7 also, we will not get the error even when the end value is not given, it will print the elements from index 2 to end index of the list with step 2.

In code line 8, we haven't given any value to start, end, and step. It will consider default values and print all elements in the list.

In code line 9, we haven't given the start and step value, the end value is 25 which is out of the index. It will consider the default values and print the elements

In code line 10, we gave start value 25, it will print the empty list because python index number 25 is not present in the list.

Once we run the program we will get the following output.


The original list is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The sliced list are:
[3, 5]
[5, 6, 7, 8, 9, 10]
[3, 5, 7, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[]

Example: Slice with Positive and Negative index.

We can slice the list by giving both a positive index and a negative index.

#Intializing list
list_1=[1,2,3,4,5,6,7,8,9,10]
print("The original list is:",list_1)
print("The sliced list are:")
print(list_1[1:-2])
print(list_1[6:-1:])
print(list_1[0:-6])

Once we run the program we will get the following output.


The original list is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The sliced list are:
[2, 3, 4, 5, 6, 7, 8]
[7, 8, 9]
[1, 2, 3, 4]

Example: Slice with negative index.

We can slice the list with a negative index. The negative index starts with -1. The below example shows, how to extract elements with a negative index.

#Intializing list
list_1=[1,2,3,4,5,6,7,8,9,10]
print("The original list is:",list_1)
print("The sliced list are:")
print(list_1[-5:-2])
print(list_1[-1::2])
print(list_1[-7:-6])

Once we run the program we will get the following output.


The original list is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The sliced list are:
[6, 7, 8]
[10]
[4]

Example: Reversing the list using the slice operator.

We can reverse the list by using the slice operator. All we need is to give a negative value to step.

#Intializing list
list_1=[1,2,3,4,5,6,7,8,9,10]
print("The original list is:",list_1)
print("The sliced list are:")
print(list_1[-1:-11:-1])
print(list_1[-1::-1])
print(list_1[::-1])

Once we run the program we will get the following output.


The original list is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The sliced list are:
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Conclusion:

In this tutorial, we learned about the slice operator and it works. We solved examples by giving positive index and negative index.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.