Preface:
List elements can be added or deleted.this articleIntroduces several ways to add elements, although they are all increased but also different, here introduced to delete the list of elements of the same method, the following is a demonstration.
i. del delete list
del is not a method, it's a keyword in Python that's specialized to perform deletion operations, which can delete not only entire lists, but also certain elements of a list. Not only can you delete individual elements, but you can also delete segments of elements. And del removes lists or list elements directly from memory.
Let's first look at the results returned by deleting the entire list:
name1 = ['python', 'java', 'php', 'MySql', 'C++', 'C', 'C#'] print(name1) del name1 print(name1)
Returns results:
['python', 'java', 'php', 'MySql', 'C++', 'C', 'C#']
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/python Knowledge Summary/python Basics/9-3.Removing List Elements.py", line 5, in <module>
print(name1)
NameError: name 'name1' is not defined
Here the return result isname1
There are no definitions, indicating that the list has been completely deleted.
The following removes the element with the specified index value:
name1 = ['python', 'java', 'php', 'MySql', 'C++', 'C', 'C#'] del name1[3] print(name1)
Returns results:
['python', 'java', 'php', 'C++', 'C', 'C#']
Delete from the back in reverse index:
name1 = ['python', 'java', 'php', 'MySql', 'C++', 'C', 'C#'] del name1[-3] print(name1)
Returns results:
['python', 'java', 'php', 'MySql', 'C', 'C#']
Attention:Orthogonal order is indexed from 0 and reverse order is from -1.
The following removes the specified interval element:
name1 = ['python', 'java', 'php', 'MySql', 'C++', 'C', 'C#'] del name1[3:5] print(name1)
Returns results:
['python', 'java', 'php', 'C', 'C#']
Second, the pop () method to remove the list of elements
name1 = ['python', 'java', 'php', 'MySql', 'C++', 'C', 'C#'] (0) # Delete the first element print(name1) (-1) # Delete the last element print(name1) () # Delete the last element by default print(name1)
Returns results:
['java', 'php', 'MySql', 'C++', 'C', 'C#']
['java', 'php', 'MySql', 'C++', 'C']
['java', 'php', 'MySql', 'C++']
Third, remove () method to remove the list of elements
remove()
Can only delete the specified value of the list element or the first element, the and relationship between the two conditions, that is, if there are two identical values in the list, only the first will be deleted, if the element does not exist to return to theValueError
Error.
name1 = ['python', 'java', 'php', 'MySql', 'C++', 'C', 'php', 'C#'] ('php') print(name1) ('php') print(name1) ('php') print(name1)
Returns results:
['python', 'java', 'MySql', 'C++', 'C', 'php', 'C#']
['python', 'java', 'MySql', 'C++', 'C', 'C#']
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/python Knowledge Summary/python Basics/9-3.Removing List Elements.py", line 32, in <module>
('php')
ValueError: (x): x not in list
Fourth, clear () to remove the list of elements
The above methods all remove a portion of the elements in the list.clear()
method is to empty the list of all elements.
name1 = ['python', 'java', 'php', 'MySql', 'C++', 'C', 'php', 'C#'] () print(name1)
Return results: []
to this article on python delete list elements del, pop (), remove () and clear () of the article is introduced to this, more related python delete list elements content please search my previous posts or continue to browse the following related articles I hope that you will support me in the future!