Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a list contains duplicate items?

I want to validate a list to make sure that there are no duplicate items. My problem is that I don't know how to do this in the if statement. Is there a method or something in python that will return False if there are duplicates in the list?

Here is what I had in mind:

lst = ["1","2","3","3","4"]

if #lst contains no duplicates :
    print("success")
else:
    print("duplicate found")

Thanks in advance.

like image 319
cbuch1800 Avatar asked Jan 11 '18 14:01

cbuch1800


People also ask

Can list contain duplicate items?

Python list can contain duplicate elements.


1 Answers

As said by Jkdc, convert it to a set and compare the length

lst = ["1","2","3","3","4"]

if len(set(lst)) == len(lst):
    print("success")
else:
    print("duplicate found")
like image 93
ThibThib Avatar answered Sep 21 '22 17:09

ThibThib