How can I find the duplicates in a Python list and create another list of the duplicates? The list only contains integers.
A bit late, but maybe helpful for some. For a largish list, I found this worked for me.
l=[1,2,3,5,4,1,3,1] s=set(l) d=[] for x in l: if x in s: s.remove(x) else: d.append(x) d [1,3,1]
Shows just and all duplicates and preserves order.