a=['1545','1254','1545']
from collections import Counter
print [item for item, count in Counter(a).items() if count != 1]
Output
['1545']
This solution works in O (N). This will be a huge advantage if there are many elements in the list used.
If you just want to find a duplicate list, you can just do
a=['1545','1254','1545']
from collections import Counter
print any(count != 1 for count in Counter(a).values())
As @gnibbler suggested , this would be the fastest solution
from collections import defaultdict
def has_dup(a):
result = defaultdict(int)
for item in a:
result[item] += 1
if result[item] > 1:
return True
else:
return False
a=['1545','1254','1545']
print has_dup(a)
source
share