Find duplicated elements in a python list
This is a pretty neat python snippet to find duplicated elements in a list:
def find_duplicates(my_list):
my_set = set()
tmp = seen.add
duplicates = set( x for x in my_list if x in my_set or tmp(x) )
return list( duplicates )
Try it:
my_list = [1234, 456, 1234, 678, 'abc', 'abc', 'abc']
find_duplicates(my_list) # should be [1234, 'abc']
def find_duplicates(my_list):
my_set = set()
tmp = seen.add
duplicates = set( x for x in my_list if x in my_set or tmp(x) )
return list( duplicates )
Try it:
my_list = [1234, 456, 1234, 678, 'abc', 'abc', 'abc']
find_duplicates(my_list) # should be [1234, 'abc']
Comments
Post a Comment