how to check if a list has duplicates 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: python has duplicates

def has_duplicates(lst):
    return len(lst) != len(set(lst))
    
x = [1,2,3,4,5,5]
has_duplicates(x) 			# True

Example 3: how to check if there are duplicates in a list python

>>> your_list = ['one', 'two', 'one']
>>> len(your_list) != len(set(your_list))
True