Find Duplicates In List: Python One Liner
How do you find all the duplicates from a list in Python? To find all the duplicate elements from a list using Python, use the following list comprehension: 1 [x for idx, x in enumerate(original_list) if x in original_list[idx+1:] and x not in original_list[:idx]] The result from the above code will be a list of unique elements representing all the duplicate elements from the original list. Here’s an example of how this looks in your Python console: 1 2 3 >>> my_list = [1, 1, 2, 3, 3, 3] >>> [x for idx, x in enumerate(my_list) if x in my_list[idx+1:] and x not in my_list[:idx]] [1, 3] Get List Of Duplicates What if you want a list of all the duplicate entries? ...