Check if list has duplicates
The following method checks whether a list has duplicate values by using the fact that set() contains only unique elements.
Python: 3.0    Fatos Morina    03 Jul 2021   0
Login to upvote
1
2
3
4
5
6
7
8
def has_duplicates(lst):
    return len(lst) != len(set(lst))
    
    
x = [1,2,3,4,5,5]
y = [1,2,3,4,5]
has_duplicates(x) # True
has_duplicates(y) # False
Copy | Download as: .py file   PNG
Share on: FB   Twitter   LinkedIn   WA
©2021 - code108labs   Contributors   Hosted on DigitalOcean   Partner website: pythoncircle.com