How do I find the duplicates in a list and create another list with them?

前端 未结 30 2246
梦谈多话
梦谈多话 2020-11-22 00:56

How can I find the duplicates in a Python list and create another list of the duplicates? The list only contains integers.

30条回答
  •  滥情空心
    2020-11-22 01:18

    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)

提交回复
热议问题