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.
Python list can contain duplicate elements.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With