Method 1: The easiest way
This method is based on traversing the entire list and adding the first element to the new list.
# Python 3 code to demonstrate # removing duplicated from list # using naive methods # initializing list test_list = [1, 3, 5, 6, 3, 5, 6, 1] print ("The original list is : " + str(test_list)) # using naive method to remove duplicated from list res = [] for i in test_list: if i not in res: (i) # printing list after removal print ("The list after removing duplicates : " + str(res))
Output result:
The original list is: [1, 3, 5, 6, 3, 5, 6, 1]
List after deleting duplicates: [1, 3, 5, 6]
Method 2: UnderstandList
This method is actually a simplified version of the first method. It uses a list comprehension and can replace the loop method above with a line of code.
# Python 3 code to demonstrate # removing duplicated from list # using naive methods # initializing list test_list = [1, 3, 5, 6, 3, 5, 6, 1] print ("The original list is : " + str(test_list)) # using naive method to remove duplicated from list res = [] for i in test_list: if i not in res: (i) # printing list after removal print ("The list after removing duplicates : " + str(res))
Method 3: Use set()
This is the most popular way to remove duplicate elements from a list. However, one of the biggest disadvantages of this method is that the order of elements in the list after set is no longer the same as before.
# Python 3 code to demonstrate # removing duplicated from list # using set() # initializing list test_list = [1, 5, 3, 6, 3, 5, 6, 1] print ("The original list is : " + str(test_list)) # using set()to remove duplicated from list test_list = list(set(test_list)) # printing list after removal # distorted ordering print ("The list after removing duplicates : " + str(test_list))
Output result:
The original list is: [1, 5, 3, 6, 3, 5, 6, 1]
List after deleting duplicates: [1, 3, 5, 6]
Method 4: Use List Comprehension + enumerate()
This method uses enumeration to remove duplicate elements based on list comprehension. Skip the element by checking if it already exists in the list. This method keeps the order of elements in the list.
Sample code:
# Python 3 code to demonstrate # removing duplicated from list # using list comprehension + enumerate() # initializing list test_list = [1, 5, 3, 6, 3, 5, 6, 1] print ("The original list is : " + str(test_list)) # using list comprehension + enumerate() # to remove duplicated from list res = [i for n, i in enumerate(test_list) if i not in test_list[:n]] # printing list after removal print ("The list after removing duplicates : " + str(res))
Method 5: Use ()
This is the fastest way to complete special tasks. It first deletes duplicates in the list and returns a dictionary, and finally converts it to a list. This method can also be used for strings, and the order of elements in the list changes after that.
# Python 3 code to demonstrate # removing duplicated from list # using () from collections import OrderedDict # initializing list test_list = [1, 5, 3, 6, 3, 5, 6, 1] print ("The original list is : " + str(test_list)) # using () # to remove duplicated from list res = list((test_list)) # printing list after removal print ("The list after removing duplicates : " + str(res))
Method 6: Handle duplicate elements in nested lists
Used for multi-dimensional list (list nesting) duplicate element removal. It is assumed here that elements with the same elements (but not necessarily in the same order) in a list (also a list) are considered duplicate. Then use the following set() + sorted() method to complete the task.
# Python3 code to demonstrate # removing duplicate sublist # using set() + sorted() # initializing list test_list = [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]] # printing original list print("The original list : " + str(test_list)) # using set() + sorted() # removing duplicate sublist res = list(set(tuple(sorted(sub)) for sub in test_list)) # print result print("The list after duplicate removal : " + str(res))
Output result:
Original list: [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]]
List of deduplication: [(-1, 0, 1), (1, 3, 4), (1, 2, 3)]
You can also use set() + map() + sorted()
# Python3 code to demonstrate # removing duplicate sublist # using set() + map() + sorted() # initializing list test_list = [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]] # printing original list print("The original list : " + str(test_list)) # using set() + map() + sorted() # removing duplicate sublist res = list(set(map(lambda i: tuple(sorted(i)), test_list))) # print result print("The list after duplicate removal : " + str(res))
Output result:
Original list: [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]]
List of deduplication: [(-1, 0, 1), (1, 3, 4), (1, 2, 3)]
This is the end of this article about six ways to delete duplicates from a List. For more related content on Python List, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!