How to Remove an Element from a List in Python
We can store items of several data types in an ordered sequence using the Python List data structure. Square brackets () are used to encapsulate the data, while commas are used to separate the entries (,).
Python provides numerous methods to help us remove a specific item from a list. The three methods are remove(), pop(), and clear().
Along with the approaches outlined above, we can use the del keyword to remove items from a list.
Python remove() method
A built-in method in Python that we can use with the list data type is remove(). It helps to eliminate the first item from the list that matches the given item.
element:- The item of the list we would like to remove.
When there are duplicate elements in a list, the first item that matches the provided item is eliminated. If the supplied element does not exist in the list, an exception will be thrown stating that the element does not exist in the list. There is no value returned by the delete () function. The value is passed as a parameter to delete(). Therefore it must be of the right datatype.
Initial List is : ['Javatpoint', 'Python', 'Tutorial', 'List', 'Element', 'Removal'] After using the function : ['Javatpoint', 'Tutorial', 'List', 'Element', 'Removal']
Python pop() method
Based on the provided index, the pop() function eliminates the element present at that index from the list.
index: There is only one argument, named index, for the pop() method.
We must pass the item's index to eliminate that entry from the list. The index begins at zero. Give the index as 0 to fetch the first item from the list. We can give the index as -1 to remove the last item. The index parameter is optional. In the absence of a value, the last item from the list is delivered from the list, and the parameter's default value is taken to be -1. The pop() function returns an error with the message IndexError: pop index if the supplied index is not valid or out of limits.
Initial List is : ['Python', 'Remove', 'Elements', 'List', 'Tutorial'] Element that is popped : Elements List after deleting the element ['Python', 'Remove', 'List', 'Tutorial']
Python clear() method
No value is returned by this method. The clear() function is used to empty the list().
There is no return value. The list() is emptied using clear() method.
Initial List is : ['Python', 'Remove', 'Elements', 'List', 'Tutorial'] None 
Using del Keyword
We can apply the del keyword of Python followed by the list's name to remove an item from the list. The item's index must be supplied to the list. The indexing in Python begins at zero.
We can also delete a part of the list using the del keyword. We can do this by using slicing. The keyword will erase the components within that range if the del keyword is provided with appropriate start and stop indices from the list. The syntax is as follows:
Here is an illustration of how to use del to delete the elements from the list created.
The Initial list is ['Python', 'Remove', 'Elements', 'List', 'Tutorial', 'Clear', 'Pop', 'Remove', 'Delete'] After removing the first element new list is ['Remove', 'Elements', 'List', 'Tutorial', 'Clear', 'Pop', 'Remove', 'Delete'] After removing the last element new list is ['Remove', 'Elements', 'List', 'Tutorial', 'Clear', 'Pop', 'Remove'] After removing element from index:5 ['Tutorial', 'Clear', 'Pop', 'Remove'] After removing the last 2 elements from the list ['Tutorial', 'Clear', 'Remove'] After removing elements present in the range 1:5 ['Tutorial']
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected]
Duration: 1 week to 2 week