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']