Home  >  Article  >  Backend Development  >  python remove duplicate elements from list

python remove duplicate elements from list

王林
王林Original
2020-05-13 14:41:014089browse

python remove duplicate elements from list

Can be implemented using the append method.

First set up a temporary list to save the results, and then traverse the original list from the beginning. If there is no current element in the temporary list, add it.

Specific code:

Given a list, it is required to delete duplicate elements in the list.

listA = ['python','语','言','是','一','门','动','态','语','言']
def deleteDuplicatedElementFromList2(list):
        resultList = []
        for item in list:
                if not item in resultList:
                        resultList.append(item)
        return resultList

Result:

#['python', '语', '言', '是', '一', '门', '动', '态']

Recommended tutorial: python tutorial

The above is the detailed content of python remove duplicate elements from list. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn