I have a little code that takes a list of objects, and only outputs the items in the list that are unique.
This is my code
def only_once(a):
retu
To clarify, what you want is a set of items that appear once, and only once.
The best option here is to use collections.Counter(), as it means you only count the items once, rather than once per item, greatly increasing performance:
>>> import collections
>>> {key for key, count in collections.Counter(a).items() if count == 1}
{1, 2, 3}
We simply replace the square brackets with curly braces to signify a set comprehension over a list comprehension, to get a set of results.