checking for duplicates in python code example
Example 1: count the duplicates in a list in python
some_list=['a','b','c','b','d','m','n','n']
my_list=sorted(some_list)
duplicates=[]
for i in my_list:
if my_list.count(i)>1:
if i not in duplicates:
duplicates.append(i)
print(duplicates)
Example 2: duplicate in list python
a = [1,2,3,2,1,5,6,5,5,5]
import collections
print([item for item, count in collections.Counter(a).items() if count > 1])
Example 3: python check for duplicate
def checkDuplicate(user):
if len(set(user)) < len(user):
return True
return False
Example 4: python find duplicates in string
from collections import Counter
def do_find_duplicates(x):
x =input("Enter a word = ")
for key,val in Counter(x).items():
print(key,val)