Both the pop and remove methods can delete the elements in the list, and individuals prefer to use the Remove method, because no information is printed during the deletion process and the task is done quietly.
Pop method: Information is printed during the removal process
>>> alist = ['D','D','D','D', 7, 4, 2, 1]>>> forIinchRange (len (alist) -1,-1,-1): #倒序循环ifAlist[i] = ='D': Alist.pop (i) #将index处的元素删除并return该元素'D''D''D''D'>>>alist[7, 4, 2, 1]
Remove method: The delete process does not print information
>>> alist = ['D','D','D','D', 7, 4, 2, 1]>>> forIinchRange (len (alist) -1,-1,-1):ifAlist[i] = ='D': Alist.remove ('D') #删除首次出现的值为 ' d ' element, if the element does not exist will be reported ValueError>>>alist[7, 4, 2, 1]>>> >>>
* The above test happens to be four elements connected together, if not joined together still valid.
Python deletes multiple identical elements in list