Python deletes multiple identical elements in list

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.