Skip to content

Remove duplicates from a list in Python

Advertisements
from collections import Counter

def removeDuplicates(lst):
  return [item for item, count in Counter(lst).items() if count == 1]
print(removeDuplicates([0, 1, 2, 2, 3, 4, 4, 5])) 

# [0,1, 3, 5]
from collections import Counter

def removeDuplicates(lst):
  return [item for item, count in Counter(lst).items() if count == 1]
print(removeDuplicates(["poop", "code", "code", "peep", "coder", "pip", "pulp", "kode"])) 

# ['poop', 'kode', 'coder', 'pip', 'peep', 'pulp']
See also  How to check number format exception in Java?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.