Python has a few methods to remove elements from the lists. These methods are:
-
- Python List pop() method
- The remove() method
- The del statement
Each of these is explained below with example codes so you may understand how to use them and see the difference.
What is pop method?
The pop Python method removes the element at the given position by index number.
If you do not specify the position, the last element will be removed.
How to use the pop method
The syntax of using the pop() method is:
- The i is an optional parameter that specifies the index of the list item that starts at 0. The last element is removed and returned if this is not provided. This is because the default value for this parameter is -1.
- An error will occur if the given index is not in the range.
An example of pop method with index number
An index number is given in the pop method for removing an item from the given list. The list is displayed after using the pop method along with displaying the removed item:
#pop() method demo lst_pop = [5, 10, 20, 30, 40, 50] print('List before pop() method: ', lst_pop) rmv_item = lst_pop.pop(2) print('List after pop() method with given index = 2: ', lst_pop) print('This element is removed from the list: ', rmv_item)
Output:
List after pop() method with given index = 2: [5, 10, 30, 40, 50]
This element is removed from the list: 20
Example of pop() without index
See the code and output without giving the index in the pop method:
Python program:
#pop() method demo without index lst_pop = [1, 3, 5, 7, 9, 11] print('List before pop() method: ', lst_pop) rmv_item = lst_pop.pop() print('List after pop() method without index: ', lst_pop) print('The element removed is: ', rmv_item)
Output:
List after pop() method without index: [1, 3, 5, 7, 9]
The element removed is: 11
Using negative index number example
As mentioned earlier, the default value of the index used in the pop method is -1. See this example where -1 index value is provided:
Python code:
lst_pop = [1, 3, 5, 7, 9, 11] print('List before pop() method: ', lst_pop) rmv_item = lst_pop.pop(-1) print('List after pop() method without index = -1: ', lst_pop) print('The element removed is: ', rmv_item)
Output:
List after pop() method without index: [1, 3, 5, 7, 9]
The element removed is: 11
You see, the same list is used as in the above example and it produced the same result i.e. the last element is deleted.
Python list remove method
- The list remove method also deletes the specified list item from the given list.
- It does not return any value.
- In addition, in the remove method, you provide the value that is matched and removed from the list.
- If there are multiple occurrences of the given value, it will remove only the first occurrence.
Syntax of using remove method
If x is not found in the given list, an error will be raised.
An example of using remove method
The following example shows how to use the remove method. A list of web technologies is created with four items. After that, the remove method is used for deleting the jQuery from the list. The list is displayed before and after using the Python remove() method.
#remove() demo lst_web_tech = ['HTML', 'CSS', 'JavaScript', 'jQuery'] print('Original list: ', lst_web_tech) lst_web_tech.remove('jQuery') print('After remove() method: ', lst_web_tech)
Output:
After remove() method: [‘HTML’, ‘CSS’, ‘JavaScript’]
What if a list has duplicate items?
For this example, the list is extended with duplicate items and this is followed by using the remove method. As mentioned earlier, it will only remove the first occurrence of the item. See in this example:
#remove() demo lst_web_tech = ['HTML', 'jQuery' , 'CSS', 'JavaScript', 'jQuery', 'JS', 'CSS'] print('Original list: ', lst_web_tech) lst_web_tech.remove('jQuery') print('After remove() method: ', lst_web_tech)
Output:
After remove() method: [‘HTML’, ‘CSS’, ‘JavaScript’, ‘jQuery’, ‘JS’, ‘CSS’]
You see, only the first occurrence of jQuery is removed while the second still exists after using the remove() method.
The del() statement
The del statement can also be used to remove one or more items from the specified list.
Like the pop() method, it takes the index number of the item(s) and removes it from the list.
Unlike the pop() method it does not return any value.
The del statement can also be used to clear a range of items and even the whole list.
See the examples below to learn how to do this.
Example of using del statement with index number
In this example, the del statement is used for removing the list item by index number.
A list of six integer items is created and the del statement is used for deleting the fourth element from the list which index is 3.
Program:
#del statement demo lst_del = [6, 12, 18, 24, 30, 36] print('List before del: ', lst_del) del lst_del[3] print('After using del: ', lst_del)
Result:
After using del: [6, 12, 18, 30, 36]
Removing multiple elements by using del()
You may provide the index range in the del statement and it will remove multiple items because of list slicing. See how you may remove multiple elements in this example:
Python code:
#del statement demo lst_del = [7, 14, 21, 28, 35, 42] print('List before del: ', lst_del) del lst_del[2:5] print('After list slicing (del lst_del[2:5]): ', lst_del)
Output:
You can see that three elements are removed out of six. The removal started from 2 to 5 index positions.
Clearing a complete list by using del
The entire list can be cleared by assigning the empty list to the slice in the del statement. Have a look:
lst_del_clr = [8, 16, 24, 32] print('List before del: ', lst_del_clr) del lst_del_clr[:] print('After slicing (del lst_del_clr[:]): ', lst_del_clr)
Output:
After slicing (del lst_del_clr[:]): []
You can see an empty list is displayed.