How can I find the duplicates in a Python list and create another list of the duplicates? The list only contains integers.
raw_list = [1,2,3,3,4,5,6,6,7,2,3,4,2,3,4,1,3,4,]
clean_list = list(set(raw_list))
duplicated_items = []
for item in raw_list:
try:
clean_list.remove(item)
except ValueError:
duplicated_items.append(item)
print(duplicated_items)
# [3, 6, 2, 3, 4, 2, 3, 4, 1, 3, 4]
You basically remove duplicates by converting to set (clean_list
), then iterate the raw_list
, while removing each item
in the clean list for occurrence in raw_list
. If item
is not found, the raised ValueError
Exception is caught and the item
is added to duplicated_items
list.
If the index of duplicated items is needed, just enumerate
the list and play around with the index. (for index, item in enumerate(raw_list):
) which is faster and optimised for large lists (like thousands+ of elements)