duplicate numbers in list 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: program to print duplicates from a list of integers in python
lst = [ 3, 6, 9, 12, 3, 30, 15, 9, 45, 36, 12, 12]
dupItems = []
uniqItems = {}
for x in lst:
if x not in uniqItems:
uniqItems[x] = 1
else:
if uniqItems[x] == 1:
dupItems.append(x)
uniqItems[x] += 1
print(dupItems)