Python Remove Duplicate Elements from List

Document 对象参考手册Python3 Examples

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)

Code explanation:

  1. Method 1: Usingset

    • setis 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.
  2. Method 2: Using list comprehension

    • List comprehension is a concise way to create lists. We iterate overoriginal_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.
  3. Method 3: Usingdict.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).

Document 对象参考手册Python3 Examples

Other Extensions