Deleting elements in a Python list is divided into the following three scenarios:
del is a keyword in Python that is specifically used to perform delete operations. It can delete not only the entire list, but some elements in the list. We have already explained how to delete entire lists in Python Lists, so this section only explains how to delete list elements.
del removes a single element from a list in the format:
del listname [index]
Among them, listname represents the name of the list, and index represents the index value of the element.
del can also delete the middle consecutive element, the format is:
del listname [start: end]
Among them, start indicates the start index and end indicates the end index. del deletes elements from index start to end, excluding elements at end position.
[Example] Use del to delete a single list element:
name = ["Jason", "Alice", "Mark", "Iven", "Peggy"] #Use positive index del name [2] print (name) #Use negative index del name [-2] print (name)
The output is:
['Jason', 'Alice', 'Iven', 'Peggy']
['Jason', 'Alice', 'Peggy']
[Example] Use del to delete a continuous section of elements:
name = ["Jason", "Alice", "Mark", "Iven", "Peggy"] del name [1: 3] print (name) name.extend (["Nicole", "Christ"]) del name [-5: -2] print (name)
The output is:
['Jason', 'Iven', 'Peggy']
['Nicole', 'Christ']
The Python pop method is used to delete the element at the specified index in the list, the specific format is as follows:
listname.pop (index)
Among them, listname represents the name of the list and index represents the index value. If you do not write the index parameter, the last element in the list is deleted by default, similar to the "pop" operation in the data structure.
Example of pop usage:
name = ["Jason", "Alice", "Mark", "Iven", "Peggy"] name.pop(3) print(name) name.pop() print(name)
The output is:
['Jason', 'Alice', 'Mark', 'Peggy']
['Jason', 'Alice', 'Mark']
Most programming languages provide a method corresponding to pop method, which is push method, which is used to add elements to the end of the list, similar to the "push" operation in a data structure. But Python is an exception. Python does not provide a push method, because you can use append method instead of push method.
In addition to the del keyword, Python also provides a remove () method, which performs a delete operation based on the value of the element itself.
It should be noted that the remove () method will only delete the first element with the same value as the specified value, and must ensure that the element exists, otherwise it will raise a ValueError.
Remove method usage example:
name = ["Jason", "Alice", "Mark", "Iven", "Peggy"] #First delete Alice name.remove ("Alice") print (name) #Second delete Mark name.remove ("Mark") print (name) #Delete Iven name.remove ("Nicole") print (name)
The output is:
['Jason', 'Mark', 'Iven', 'Peggy']
['Jason', 'Iven', 'Peggy']
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
[ipython-input-75-0c51f3cd30e5] in [module]
7 print (name)
8 #Delete Iven
----> 9 name.remove ("Nicole")
10 print (name)
ValueError: list.remove(x): x not in list
The last deletion, because Nicole does not exist and caused an error, so we better judge in advance when using remove method to delete an element.
Python clear () is used to delete all elements of the list, that is, to clear the list. Please see the following code:
website= list("www.freelearningpoints.com") website.clear() print(website)
The output is:
[]
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises