Python Remove Duplicate Elements from List
In Python, we can use multiple methods to remove duplicate elements from a list. Common methods include usingsetdata structures, list comprehensions, anddict.fromkeys()methods. Below we will show how to use these methods to remove duplicate elements from a list.
Example
# Method 1: Using set
original_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = list(set(original_list))
print("Method 1 result:", unique_list)
# Method 2: Using list comprehension
original_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = []
[unique_list.append(x) for x in original_list if x not in unique_list]
print("Method 2 result:", unique_list)
# Method 3: Using dict.fromkeys()
original_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = list(dict.fromkeys(original_list))
print("Method 3 result:", unique_list)
original_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = list(set(original_list))
print("Method 1 result:", unique_list)
# Method 2: Using list comprehension
original_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = []
[unique_list.append(x) for x in original_list if x not in unique_list]
print("Method 2 result:", unique_list)
# Method 3: Using dict.fromkeys()
original_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = list(dict.fromkeys(original_list))
print("Method 3 result:", unique_list)
Code explanation:
-
Method 1: Using
setsetis a data structure in Python that does not allow duplicate elements. By converting the list to aset, we can automatically remove duplicate elements, and then convert it back to a list.list(set(original_list))willoriginal_listConvert toset, and then convert back to a list.
-
Method 2: Using list comprehension
- List comprehension is a concise way to create lists. We iterate over
original_list, and elements not inunique_listare added tounique_list. [unique_list.append(x) for x in original_list if x not in unique_list]This line of code iterates overoriginal_list, and adds non-duplicate elements tounique_list.
- List comprehension is a concise way to create lists. We iterate over
-
Method 3: Using
dict.fromkeys()dict.fromkeys()The method creates a dictionary, and dictionary keys are unique, so duplicate elements can be removed. Then we convert the dictionary keys back to a list.list(dict.fromkeys(original_list))willoriginal_listConvert to dictionary keys, and then convert back to a list.
Output result:
方法1结果: [1, 2, 3, 4, 5] 方法2结果: [1, 2, 3, 4, 5] 方法3结果: [1, 2, 3, 4, 5]
All methods successfully removed duplicate elements from the list and preserved the order of the original list (except Method 1, becausesetdoes not guarantee order).
Python3 Examples