How do I find the duplicates in a list and create another list with them?
List of duplicates using collections
import collections thelist = [1, 2, 3, 4, 4, 5, 5, 6, 1] print([item for item, count in collections.Counter(thelist).items() if count > 1])
[1, 4, 5]
List of duplicates using iteration_utilities
from iteration_utilities import duplicates thelist = [1, 2, 3, 4, 4, 5, 5, 6, 1] print(list(duplicates(thelist))) thelist = ['A', 'B', 'C', 'D', 'A'] print(list(duplicates(thelist)))
[4, 5, 1] ['A']