List Methods In Python


In my previous article, Introduction to Python List, I explained the basics of lists and a few methods such as Index(), Append(), and Extend(). Now, let us understand the remaining List methods.


The Insert method is used to insert an element into a list. It accepts two parameters, i.e.,
  1. The index which specifies the index of an element where you want to insert an element.
  2. Element is an element that you want to insert into the list.
Syntax- list.insert (index,element)
  1. l=[2,4,6]    
  2. l.insert(2,14)     
  3. print(l)              # will print [2,4,14,6]  
It accepts only two parameters; otherwise, it generates an error.
list_methods_in python


This method is used to delete an element from the list from the end. It takes an index as an optional parameter. That is, if you don't pass any argument then it will remove the item from the end of the list; otherwise, it will remove the item from the specified position.
Syntax- list.pop()
  1. l=[2,3,8,7,9,6,5]    
  2. l.pop()    
  3. print(l)         # will print [2,3,8,7,9,6]    
  4. l.pop(3)    
  5. print(l)         # will print [2,3,8,9,6]   


This method is used to delete an element from the List. However, it is different from the pop() method. Instead of deleting an element from the index, it will delete an element as per argument. That is, we can pass an element as an argument to be deleted. 
Syntax- list.remove(element)
  1. l=[2,3,8,7,9,6,5]      
  2. l.remove(3)      
  3. print(l)         # will print [2,8,7,9,6,5]     
If the element is not present in the list, then it will generate an error.


This method is used to clear the whole list, i.e., it removes all the items from the list.
Syntax- list.clear() 
  1. l=[2,3,8,7,9,6,5]        
  2. l.clear()        
  3. print(l)         # will print []    


We can reverse the order of items in a list by using this method.
Syntax- list.reverse()
  1. l=[2,3,8,7,9,6,5]          
  2. l.reverse()          
  3. print(l)         # will print [5,6,9,7,8,3,2]    


This method is used to sort a list. By default, the list is sorted in ascending order. We can also sort a list in descending order, bypassing "reverse=True" as an argument in sort().
Syntax- list.sort() // Ascending order
list.sort(reverse=True) // Descending order
  1. l=[2,3,8,7,9,6,5]            
  2. l.sort()            
  3. print(l)         # will print [2,3,5,6,7,8,9]      
  4. l.sort(reverse="True")    
  5. print(l)         # will print [9,8,7,6,5,3,2]  


This method will return the number of times the element occurs within a specified list. It takes one argument, as an element to be counted.
Syntax- list.count(element)
  1. l=[2,3,8,3,7,9,3,6,5]              
  2. l.count(3)              
  3. print(l)         # will print 3   


In this article, we discussed all the methods of List. I hope this will help the readers to understand how to use and implement Lists in Python.
Feedback or queries related to this article are most welcome. Thanks for reading.