Python provides three common methods for deleting duplicate elements in the list.

Source: Internet
Author: User
Tags python list

Python provides three common methods for deleting duplicate elements in the list.

This example describes three common methods for deleting duplicate elements in the Python list. We will share this with you for your reference. The details are as follows:

If a list is specified, the repeated elements in the list must be deleted.

ListA = ['python', 'yue', 'yan', 'yes', 'yi', 'Gateway', 'Action', 'status', 'yue ', 'yan']

Method 1To sort the list, and compare two adjacent elements from the end in sequence. If duplicate elements exist, delete them. Otherwise, move the pointer to the left to repeat the above process:

def deleteDuplicatedElementFromList(list):    list.sort();    print("sorted list:%s" % list)    length = len(list)    lastItem = list[length - 1]    for i in range(length - 2,-1,-1):        currentItem = list[i]        if currentItem == lastItem:            list.remove(currentItem)        else:            lastItem = currentItem    return list

Method 2, Set a temporary list to save the result and traverse the original list from the beginning. If the temporary list does not contain the current element, append it:

def deleteDuplicatedElementFromList2(list):    resultList = []    for item in list:        if not item in resultList:            resultList.append(item)    return resultList

Method 3To convert the list into a set by using the unique feature of elements in python, and then convert it to a list and return:

def deleteDuplicatedElementFromList3(listA):    #return list(set(listA))    return sorted(set(listA), key = listA.index)

Execution result:

Print (deleteDuplicatedElementFromList (listA) # sorted list: ['python', '1', 'dynamic ', 'status', 'yes', 'yan', 'yan ', 'output', 'output'] # ['python', 'yi', 'in', 'status', 'yes', 'any ', 'out', 'ware'] print (deleteDuplicatedElementFromList2 (listA) # ['python', 'out', 'yes', '1 ', 'Gateway', 'mobile', 'status'] print (deleteDuplicatedElementFromList3 (listA) # ['python', 'out', 'any', 'yes ', '1', 'door', 'Action', 'status']

Analysis:

Method 1: The logic is complex. Saving values of temporary variables consumes memory. The returned results destroy the original list order, with the worst efficiency.
Method 2: directly call the append method to modify the list. The logic is clear and the efficiency is secondary.
Method 3: extremely concise, with the highest efficiency in using python native Methods

PS: Here are several repeated tools for your reference:

Online deduplication tools:
Http://tools.jb51.net/code/quchong

Online text deduplication tool:
Http://tools.jb51.net/aideddesign/txt_quchong

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.