How to slice a list in Python

In this post, we will learn how to slice a list in Python with examples. By using indexing, we can select/change/delete a single element. But Sometimes we have to select/change/modify/delete a range of elements from the python list then slicing is used. The slicing is represented By using a colon(:) also known as a slicing operator. The syntax to use the slicing operator

Syntax

The: slicing operator is used to do slicing in Python. It returns the new list of elements that pass the given slicing range from the existing list.

list[start:stop: step]


#some mostly used slining syntax

list[start:stop]  # slicing start from start index and stop-1
list[start:]      # get item from start to stop
list[:stop]       # get items from the start to stop-1
list[:]           # get all element in a list
  • start: The index of the list at where to start the slicing.
  • stop: The index at slicing will stop.
  • step: Select nth element within start and stop range.

Program Examples of Below Syntax

pre_List = ['at','on','into','up','The',2,5,6]

#here the syntax : list[start:stop]
print(pre_List[1:3])

#here the syntax :  list[start:] 
print(pre_List[1:])  

#here the syntax :  list[:stop]    
print(pre_List[:5])  
  
#here the syntax :  list[:]    
print(pre_List[:]) 

Output

['on', 'into']
['on', 'into', 'up', 'The', 2, 5, 6]
['at', 'on', 'into', 'up', 'The']
['at', 'on', 'into', 'up', 'The', 2, 5, 6]

1. How to slice a list in Python from start to end range


In this example, we are slicing the list starting from index 1 and stopping at index 6. The element at index 6 will not be included in the resulting output.

pre_List = ['at','on','into','up','The',2,5,6]
 
#start from index 1 to end at 6, element at index 6 (will not be included)

print(pre_List[1:6])


Output

['on', 'into', 'up', 'The', 2]

2. How to slice a list in Python a start index to end


In this example, we are accessing the whole list by using the slicing operator [:] that will return a copy of the whole list. The pre_List[1:] slicing of the list element starts from index 1 to the end of the list.

pre_List = ['at','on','into','up','The',2,5,6]
 
 
#start from index 0 to end at end of list or whole list

print('whole list :',pre_List[:])

#start from index 1 to end at end of list the element at end index will not included
print('start from index 1 to end:',pre_List[1:])



Output

whole list : ['at', 'on', 'into', 'up', 'The', 2, 5, 6]
start from index 1 to end: ['on', 'into', 'up', 'The', 2, 5, 6]

3. How to slice a list in Python before some index using slicing


In this example, we are accessing an element starting from index 0 to index 4. The element at index 4 will not be included in output result

pre_List = ['at','on','into','up','The',2,5,6]
 
 
#start from index 0 to index 4
print('Access element before some index :',pre_List[:4])

Output

Access element before some index : ['at', 'on', 'into', 'up']

4. Access list elements between some intervals using Slicing


In this example, we are accessing the list elements by using start, stop and step. The slicing starts at index 0 and increments by 1 every step and stops at index 5.

pre_List = ['at','on','into','up','The',2,5,6]
 
 


print('slicing list using stat,stop,step:',pre_List[1:5:1])

Output

slicing list using stat,stop,step: ['on', 'into', 'up', 'The']

5. Access list element using negative slicing end to start


In this example, we will understand how to access the list element by using negative slicing using code pre_List[:-1] that will slice the list starting from index 0 to the end of the list.

pre_List = ['at','on','into','up','The',2,5,6]
 

print('slicing list using negative index:',pre_List[:-1])

Output

slicing list using negative index: ['at', 'on', 'into', 'up', 'The', 2, 5]

6. Access list element using a range of negative slicing


In this example, we are accessing a range of list elements using negative indexing starting from index -1 to stop at index -5. To access all list elements by negative index, we have to use pre_List[-6:]. Let us understand with the below example

pre_List = ['at','on','into','up','The',2,5,6]

print('slicing list using negative index:',pre_List[-6:]) 

print('slicing list using negative index:',pre_List[-5:-1])

Output

slicing list using negative index: ['into', 'up', 'The', 2, 5, 6]
slicing list using negative index: ['up', 'The', 2, 5]

7. Access list elements negative and positive slicing


In this example, we are accessing the list element by using positive and negative slicing by using code pre_List[1:-5]

pre_List = ['at','on','into','up','The',2,5,6]

print('slicing list using negativ and postive index:',pre_List[1:-5])

Output

slicing list using negativ and postive index: ['on', 'into']