How can I find the duplicates in a Python list and create another list of the duplicates? The list only contains integers.
I guess the most effective way to find duplicates in a list is:
from collections import Counter
def duplicates(values):
dups = Counter(values) - Counter(set(values))
return list(dups.keys())
print(duplicates([1,2,3,6,5,2]))
It uses Counter once on all the elements, and then on all unique elements. Subtracting the first one with the second will leave out the duplicates only.