How to Remove an Element from a List in PythonWe 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. 1. Python remove() MethodA 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. Syntax: 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. Code Output: Initial List is : ['Javatpoint', 'Python', 'Tutorial', 'List', 'Element', 'Removal'] After using the function : ['Javatpoint', 'Tutorial', 'List', 'Element', 'Removal'] 2. Python pop() MethodBased on the provided index, the pop() function eliminates the element present at that index from the list. Syntax 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. Code Output: Initial List is : ['Python', 'Remove', 'Elements', 'List', 'Tutorial'] Element that is popped : Elements List after deleting the element ['Python', 'Remove', 'List', 'Tutorial'] 3. Python clear() MethodNo value is returned by this method. The clear() function is used to empty the list(). Syntax: No parameters. There is no return value. The list() is emptied using clear() method. Code Output: Initial List is : ['Python', 'Remove', 'Elements', 'List', 'Tutorial'] None [] 4. Using del KeywordWe 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. Syntax: 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: Syntax: Here is an illustration of how to use del to delete the elements from the list created. Code Output: 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'] 5. Using list comprehensionIn Python, you can also use a list comprehension to exclude a particular element from a list. Each list item is repeated in the list comprehension. The list comprehension analyses each element to see whether it differs from the provided element. The element is added to the new list if it differs from the provided element. The element is not included to the new list if it is identical to the requested element. A new list that includes every element from the original list except the one we specified in the condition is produced as a consequence of the list comprehension. The original list is not updated because this approach generates a new list. As seen in the example, you may assign the new list back to the existing list if you wish to change it. Here's an illustration: Code Output: [1, 2, 4, 5] 6. Using filter() methodIn Python, we may also take an element out of a list by using the filter() function. The original list's items that meet the specified criterion are the only elements in the new list that are created by the filter() function. The requirement in this instance is that the element not be identical to the element you wish to delete. Here's an illustration: Code Output: [1, 2, 4, 5] 7. Using discard() methodIn Python, we may also eliminate an entry from a list by using the discard() function. If the element is present, the discard() function removes it from the list. If the element is missing from the list, the procedure returns immediately without throwing an exception. Here's an illustration: Code Output: [1, 2, 4, 5] In this case, the list's initial instance of the element 3 is removed using the discard() function. The method just returns without taking any action if the element is not present in the list. In contrast to remove(), which produces a ValueError if an element is missing from the list, discard() does not. This is an important distinction to make. 8. Using slice operatorWith the exception of the element at the given index, a new list made with the slice operator contains every element from the original list. The index of the element to be removed is stored in the index to remove variable. The slice operator generates two new lists: my_list[:index to remove] and my_list[index to remove + 1:], respectively. The first list includes all the entries before the index to remove. To generate the final list, the two new lists are concatenated together using the + operator. The original list is not updated because this approach generates a new list. As seen in the example, you may assign the new list back to the existing list if you wish to change it. Code Output: [1, 2, 4, 5] 9. Using NumPy LibraryThe original list is converted into a NumPy array using the NumPy library. The element at the given index is then deleted from the array using the delete() method. The tolist() function is used to turn the output array back into a list, which is then saved in my list variable. It should be noted that the NumPy library has a number of methods for manipulating arrays, including deleting elements from an array. The original list is not changed because a new list is created using this procedure. As illustrated in the example below, you may assign the new list back to the existing list if you wish to change it. Code Output: [1, 2, 4, 5] Next TopicHow to unpack a dictionary in python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India