how to print duplicate elements in list in python code example
Example 1: how to make python remove the duplicates in list
mylist = ["a", "b", "a", "c", "c"]
mylist = list(dict.fromkeys(mylist))
print(mylist)
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: 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)