Slicing list in Python

With the help of slicing, we can get the required elements from the list (sublist).

Slicing won't affect the original list. It will return the new list with the required elements.

Example

list[start index : end index]

starting index value will be included in the list and the ending index value will be excluded from the list.


Example

#Slice the lists in python

numbers =       [10, 20, 30, 40, 50]
#forward index   0    1   2   3   4
#reverse index  -5   -4  -3  -2  -1

print(numbers[0:3])   #from index 0 to 2. [10, 20, 30] (index 3 i.e 40 excluded)
print(numbers[-4:-1]) #from index -4 to -2. [20, 30, 40] (index -1 i.e 50 excluded)

numbers[0:3] will return the new sublist [10, 20, 30]. Index 3 i.e. value 40 will be excluded.

numbers[-4:-1] will return the new sublist [20, 30, 40]. Index -1 i.e value 50 will be excluded.




Slicing with default values

We can also use the default value in slicing by omitting either start index or end index.

The omitted first index will be set to 0.

The omitted end index will be set to length of the list (not end index of the list).


numbers = [10, 20, 30, 40, 50]

numbers[:index] ===> numbers[0:index]
numbers[index:] ===> numbers[index:5] #length of the list is 5.

Example

#Slice the lists in python

numbers =       [10, 20, 30, 40, 50]
#forward index   0    1   2   3   4
#reverse index  -5   -4  -3  -2  -1

#from index 0 to 1. [10, 20]    (index 2 i.e 30 excluded)
print("Index 0 to 1 = ", numbers[:2])

#from index 2 to the end. [30, 40, 50] (index 2 i.e 30 included )
print("Index 2 to 4 = ", numbers[2:])      

#which makes sure that numbers[:index] + numbers[index:] is always equal to numbers
print("List elements are ", numbers[:2]+numbers[2:])

#from index -5 to -4. [10, 20] (index -3 i.e 30 excluded)
print("Index -5 to -4 = ", numbers[:-3])   
#from index -3 to -1. [30, 40, 50] (index -3 i.e. 30 included)
print("Index -3 to -1 = ", numbers[-3:])  
print("List elements are ", numbers[:-3]+numbers[-3:])




Copy of the List

We can get the copy of the whole list by using list[:].

This is very useful when we want to manipulate the list but don't want to change the actual contents of it.


Example

#new copy of the list

numbers = [1, 2, 3]
numbers_copy = numbers[:]  #numbers[:] will return the new copy of the list

print("Copy of the list ", numbers_copy)




Assignment to Slices

Assigning values to the sliced sublist.


Example

#Assignment to slices

numbers = [1, 2, 3, 4, 5]
numbers[0:3] = [-1, -2, -3] #changing values from index 0 to 2 {0, 1, 2}

print(numbers)




Removing Elements from the List

We can remove any sublist from the list by replacing all the sublist elements with an empty list.


Example

#Removing elements from the list

numbers = [10, 20, 30, 40, 50]
numbers[1:3] = []  #Removing elements from index 1 to 2. i.e {20,30}

print(numbers)




Clear the Whole List

We can also clear the whole list by replacing all the elements with an empty list.


Example

#Clear the whole list

numbers = [1, 2, 3, 4, 5]
numbers[:] = []

print(numbers)