How to remove duplicates in python

爱喝马黛茶的安东尼
Release: 2019-06-21 11:35:15
Original
8829 people have browsed it

How to remove duplicates in python

How to remove duplicates in python? Here are several python methods to remove duplicates:

Method 1: Use the built-in set method to remove duplicates

>>> lst1 = [2, 1, 3, 4, 1]
>>> lst2 = list(set(lst1))
>>> print(lst2)
[1, 2, 3, 4]
Copy after login

Method 2: Use the fromkeys() method in the dictionary to remove duplicates

>>> lst1 = [2, 1, 3, 4, 1]
>>> lst2 = {}.fromkeys(lst1).keys()
>>> print(lst2)
dict_keys([2, 1, 3, 4])
Copy after login

Related recommendations: "python video tutorial"

Method 3: Use conventional methods to remove duplicates

>>> lst1 = [2, 1, 3, 4, 1]
>>> temp = []
>>> for item in lst1:
    if not item in temp:
    temp.append(item)
>>> print(temp)
[2, 1, 3, 4]
Copy after login

Method 4: Use list comprehension to remove duplicates

>>> lst1 = [2, 1, 3, 4, 1]
>>> temp = []
>>> [temp.append(i) for i in lst1 if not i in temp]
[None, None, None, None]
>>> print(temp)
[2, 1, 3, 4]
Copy after login

Method 5: Use the sort function to remove duplicates

>>> lst1 = [2, 1, 3, 4, 1]
>>> lst2.sort(key=lst1.index)
>>> print(lst2)
[2, 1, 3, 4]
Copy after login

Method 6: Use the sorted function to remove duplicates

>>> lst1 = [2, 1, 3, 4, 1]
>>> lst2 = sorted(set(lst1), key=lst1.index)
>>> print(lst2)
[2, 1, 3, 4]
Copy after login

Note: Some of the previous methods cannot guarantee their order. For example, use the set() function to handle it!

If you want to delete duplicate items in the list, you can also use the following methods to handle it

>>> # Method 1:

>>> data = [2, 1, 3, 4, 1]
>>> [item for item in data if data.count(item) == 1]
Copy after login

[2, 3, 4]

>>> # Method 2:

>>> data = [2, 1, 3, 4, 1]
>>> list(filter(lambda x:data.count(x) == 1, data))
[2, 3, 4]
Copy after login

The above is the detailed content of How to remove duplicates in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!