Python's way of removing duplicate elements from a list

  • 2020-04-02 14:43:47
  • OfStack

This example shows how Python removes duplicate elements from a list. Share with you for your reference. The details are as follows:

The easier thing to remember is to use the built-in set


l1 = ['b','c','d','b','c','a','a']
l2 = list(set(l1))
print l2

Another, which is said to be faster, has not been tested for speed differences between the two


l1 = ['b','c','d','b','c','a','a']
l2 = {}.fromkeys(l1).keys()
print l2

One drawback to both is that the order changes after the repetition element is removed:


['a', 'c', 'b', 'd']

If you want to keep them in their original order:

Use the sort method of the list class


l1 = ['b','c','d','b','c','a','a']
l2 = list(set(l1))
l2.sort(key=l1.index)
print l2

Or you could write it like this


l1 = ['b','c','d','b','c','a','a']
l2 = sorted(set(l1),key=l1.index)
print l2

You can also use traversal


l1 = ['b','c','d','b','c','a','a']
l2 = []
for i in l1:
  if not i in l2:
    l2.append(i)
print l2

The above code can also be written like this


l1 = ['b','c','d','b','c','a','a']
l2 = []
[l2.append(i) for i in l1 if not i in l2]
print l2

This ensures that the order is constant:


['b', 'c', 'd', 'a']

I hope this article has helped you with your Python programming.


Related articles: