How to remove duplicates from a list in Python

Filters out the unique values in a list.

-Use a collections.Counter to get the count of each value in the list.

-return a list containing only the unique values.

from collections import Counter

def filter_unique(lst):
  return [item for item, count in Counter(lst).items() ]
filter_unique([1, 2, 2, 3, 4, 4, 5]) # [1,2,3,4,5]

Maher Gallaoui

You like our Content? Follow us to stay up-to-date.