Sayantan's Blog On Python Programming

PYTHON LIST SLICING OPERATION

PYTHON LIST SLICING OPERATION

INTRODUCTION

Before understanding slicing operation lets look at some list accessing methods by using index values.

# Accessing list elements example 1

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of list int_list at index 0->", int_list[0]) 
print("Element of list int_list at index 2->", int_list[2]) 

print("\nElement of list int_list at index -2->", int_list[-2]) 
print("Element of list int_list at index -4->", int_list[-4]) 

PYTHON LIST SLICING OPERATION : Output

Element of list int_list at index 0-> 400
Element of list int_list at index 2-> 600

Element of list int_list at index -2-> 150
Element of list int_list at index -4-> 600

In the above example, we have used positive index as well as negative index. Negative index will be counted from the end of the list. In the above case, -2 will return the 2nd last element 150 and -4 will return 4th last element 600.

LIST SLICING OPERATION

POSITIVE SLICING OPERATION

Now, Slicing operation means accessing a range of elements in a list.

Syntax: list_name[start_index : end_index]

Start index is inclusive i.e. the index position of the list and end index is exclusive i.e. it will go to the index position end index – 1.

# Slicing list elements example 1

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)
int_list_slice = int_list[0:4]
print("\nElement of sliced list ->", int_list_slice)
print("\nElement of list int_list_slice at index 0->", int_list_slice[0]) 
print("Element of list int_list_slice at index 1->", int_list_slice[1]) 
print("Element of list int_list_slice at index 2->", int_list_slice[2])
print("Element of list int_list_slice at index 3->", int_list_slice[3]) 

PYTHON LIST SLICING OPERATION : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sliced list -> [400, 200, 600, 100]

Element of list int_list_slice at index 0-> 400
Element of list int_list_slice at index 1-> 200
Element of list int_list_slice at index 2-> 600
Element of list int_list_slice at index 3-> 100

In the above example, we have taken start index as 0 and end index as 4. That means slicing operation will be start from index position 0 and from there it will fetch 4 elements so will be ended at index position 3. Here end index refer to the index position end index – 1 i.e. 3.

# Slicing list elements example 2

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)
int_list_slice = int_list[0:2]
print("\nElement of sliced list ->", int_list_slice)
print("\nElement of list int_list_slice at index 0->", int_list_slice[0]) 
print("Element of list int_list_slice at index 1->", int_list_slice[1]) 

PYTHON LIST SLICING OPERATION : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sliced list -> [400, 200]

Element of list int_list_slice at index 0-> 400
Element of list int_list_slice at index 1-> 200

If we want to access all the elements of a list then we either can put the total number of elements in the end index or we can leave the end index position as null.

# Slicing list elements example 3

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)
int_list_slice = int_list[0:6]
print("\nElement of sliced list ->", int_list_slice)
print("\nElement of list int_list_slice at index 0->", int_list_slice[0]) 
print("Element of list int_list_slice at index 1->", int_list_slice[1]) 
print("Element of list int_list_slice at index 2->", int_list_slice[2]) 
print("Element of list int_list_slice at index 3->", int_list_slice[3]) 
print("Element of list int_list_slice at index 4->", int_list_slice[4]) 
print("Element of list int_list_slice at index 5->", int_list_slice[5]) 

PYTHON LIST SLICING OPERATION : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sliced list -> [400, 200, 600, 100, 150, 500]

Element of list int_list_slice at index 0-> 400
Element of list int_list_slice at index 1-> 200
Element of list int_list_slice at index 2-> 600
Element of list int_list_slice at index 3-> 100
Element of list int_list_slice at index 4-> 150
Element of list int_list_slice at index 5-> 500

# Slicing list elements example 4

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)
int_list_slice = int_list[0:]
print("\nElement of sliced list ->", int_list_slice)
print("\nElement of list int_list_slice at index 0->", int_list_slice[0]) 
print("Element of list int_list_slice at index 1->", int_list_slice[1]) 
print("Element of list int_list_slice at index 2->", int_list_slice[2]) 
print("Element of list int_list_slice at index 3->", int_list_slice[3]) 
print("Element of list int_list_slice at index 4->", int_list_slice[4]) 
print("Element of list int_list_slice at index 5->", int_list_slice[5]) 

PYTHON LIST SLICING OPERATION : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sliced list -> [400, 200, 600, 100, 150, 500]

Element of list int_list_slice at index 0-> 400
Element of list int_list_slice at index 1-> 200
Element of list int_list_slice at index 2-> 600
Element of list int_list_slice at index 3-> 100
Element of list int_list_slice at index 4-> 150
Element of list int_list_slice at index 5-> 500

If we put the end index as a number beyond the max limit of the list then also the entire list will be printed.

# Slicing list elements example 5

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)
int_list_slice = int_list[0:7]
print("\nElement of sliced list ->", int_list_slice)
print("\nElement of list int_list_slice at index 0->", int_list_slice[0]) 
print("Element of list int_list_slice at index 1->", int_list_slice[1]) 
print("Element of list int_list_slice at index 2->", int_list_slice[2]) 
print("Element of list int_list_slice at index 3->", int_list_slice[3]) 
print("Element of list int_list_slice at index 4->", int_list_slice[4]) 
print("Element of list int_list_slice at index 5->", int_list_slice[5]) 

PYTHON LIST SLICING OPERATION : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sliced list -> [400, 200, 600, 100, 150, 500]

Element of list int_list_slice at index 0-> 400
Element of list int_list_slice at index 1-> 200
Element of list int_list_slice at index 2-> 600
Element of list int_list_slice at index 3-> 100
Element of list int_list_slice at index 4-> 150
Element of list int_list_slice at index 5-> 500

If we put the start index as a number beyond the max limit of the list then empty list will be printed.

# Slicing list elements example 6

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)
int_list_slice = int_list[7:]
print("\nElement of sliced list ->", int_list_slice)

PYTHON LIST SLICING OPERATION : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sliced list -> []

If we want to extract the last element of the list the we can put the max index position at start index and null as end index.

# Slicing list elements example 7

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)
int_list_slice = int_list[5:]
print("\nElement of sliced list ->", int_list_slice)

PYTHON LIST SLICING OPERATION : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sliced list -> [500]

NEGATIVE SLICING OPERATION

Python allows negative slicing as well.

# Negative Slicing list elements example 1

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)
int_list_slice = int_list[0:-3]
print("\nElement of sliced list ->", int_list_slice)

PYTHON LIST SLICING OPERATION : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sliced list -> [400, 200, 600]

In the above example, we have selected the start index as 0 and end index as -3. So the 3rd last element is 100. But end index is exclusive in nature so it will go up to next index i.e. -4. The 4th last element is 600. That’s why 400, 200, 600 have been show in the result.

# Negative Slicing list elements example 2

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)
int_list_slice = int_list[-3:-1]
print("\nElement of sliced list ->", int_list_slice)

PYTHON LIST SLICING OPERATION : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sliced list -> [100, 150]

In the above example, we have taken start index as -3 which is the 3rd last element in the list i.e. 100. End index is -1. So by exclusive nature it will indicate the 2nd last element i.e. 150. That’s why the result is showing as 100 and 150.

If the range specified by slicing operation doesn’t fetch any element then python will return null.

# Negative Slicing list elements example 3

int_list = [400, 200, 600, 100, 150, 500] 
print("\nElement of original list ->", int_list)
int_list_slice = int_list[-3:-4]
print("\nElement of sliced list ->", int_list_slice)

PYTHON LIST SLICING OPERATION : Output

Element of original list -> [400, 200, 600, 100, 150, 500]

Element of sliced list -> []

In the above example we have provided start index as -3 and end index as -4. So the end index is going beyond start index. That’s why python has returned null.

PYTHON LIST SLICING OPERATION : Output
PYTHON LIST SLICING OPERATION : Output

RELATED TOPICS:

1 thought on “PYTHON LIST SLICING OPERATION”

  1. Pingback: REMOVING ELEMENT IN THE LISTS - Sayantan's Blog On Python Programming

Leave a Comment

Your email address will not be published. Required fields are marked *