[Solved] What would be the python function that returns a list of elements that only appear once in a list [duplicate]


Here is the function that you were looking for. The Iterable and the corresponding import is just to provide type hints and can be removed if you like.

from collections import Counter
from typing import Iterable
d = [1,1,2,3,4,5,5,5,6]

def retainSingles(it: Iterable):
    counts = Counter(it)
    return [c for c in counts if counts[c] == 1]

print(retainSingles(d))

Output:

[2, 3, 4, 6]

4

solved What would be the python function that returns a list of elements that only appear once in a list [duplicate]