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

前端 未结 30 2224
梦谈多话
梦谈多话 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:15

    one-liner, for fun, and where a single statement is required.

    (lambda iterable: reduce(lambda (uniq, dup), item: (uniq, dup | {item}) if item in uniq else (uniq | {item}, dup), iterable, (set(), set())))(some_iterable)
    

提交回复
热议问题