How can I find the duplicates in a Python list and create another list of the duplicates? The list only contains integers.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
One line solution:
Here's a fast generator that uses a dict to store each element as a key with a boolean value for checking if the duplicate item has already been yielded.
For lists with all elements that are hashable types:
For lists that might contain lists:
the third example of the accepted answer give an erroneous answer and does not attempt to give duplicates. Here is the correct version :
A bit late, but maybe helpful for some. For a largish list, I found this worked for me.
Shows just and all duplicates and preserves order.
Very simple and quick way of finding dupes with one iteration in Python is:
Output will be as follows:
This and more in my blog http://www.howtoprogramwithpython.com