how to find the unique items in a list in python

I have a list,

 mylist=[4, 4, 2, 1, 2]

 my expected output is =[1]

I tried list(set(mylist)) but its not helping

thanks in advance

1 answer

  • answered 2018-01-13 17:11 Moinuddin Quadri

    You may use collections.Counter to get the count of all the items present in the list, then filter it using list comprehension the items having count as 1:

    >>> from collections import Counter
    >>> mylist=[4, 4, 2, 1, 2]
    >>> [k for k, v in Counter(mylist).items() if v==1]