How can I find the duplicates in a Python list and create another list of the duplicates? The list only contains integers.
There are a lot of answers up here, but I think this is relatively a very readable and easy to understand approach:
def get_duplicates(sorted_list):
duplicates = []
last = sorted_list[0]
for x in sorted_list[1:]:
if x == last:
duplicates.append(x)
last = x
return set(duplicates)
Notes: