8

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.

Imran
  • 11,671
  • 8
  • 60
  • 75
cbuch1800
  • 833
  • 3
  • 10
  • 24
  • 3
    I would make it a set and see if the lengths are the same. – MrAlexBailey Jan 11 '18 at 14:42
  • 2
    Possible duplicate of [Removing duplicates in lists](https://stackoverflow.com/questions/7961363/removing-duplicates-in-lists) – Pavel Jan 11 '18 at 14:44
  • 1
    Make the list into a set using `set(lst)` and then compare the two lengths. If they are the same then there wont be any duplicates – wtreston Jan 11 '18 at 14:44
  • 4
    Possible duplicate of [Check for duplicates in a flat list](https://stackoverflow.com/questions/1541797/check-for-duplicates-in-a-flat-list) – EoinS Jan 11 '18 at 14:46

5 Answers5

21

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")
ThibThib
  • 7,580
  • 3
  • 28
  • 37
  • 3
    This doesn't work when `lst` is a list of dictionaries. `set(lst)` then throws a `TypeError: unhashable type: 'dict'` – the21st Oct 10 '18 at 10:28
4

Exploit the Fact that Python set may not contain duplicates. The has_duplicate() Function takes care of determining if a list contains duplicates.

def has_duplicates(listObj):
    return len(listObj) != len(set(listObj))


print(has_duplicates([1, 2, 1, 1, 4, 7]))    ## PRINTS: True
print(has_duplicates([9, 2, 5, 4, 7]))       ## PRINTS: False
Poiz
  • 7,519
  • 2
  • 13
  • 17
0

Check this, the easiest way (at least for me)..

lst = ["1","2","3","3","4"]
status = True
for item in lst:
    if lst.count(item) > 1:
      # the count functions counts how many times the "item" is in lst
        status = False

if status == True:
    print("No duplicates")
else:
    print("Duplicates found")
Abdullah
  • 131
  • 1
  • 10
0
def check_duplicates(lst):
    seen = {}
    for item in lst:
        if seen.get(item):
            print("duplicate found")
            return
        else:
            seen[item] = True
    print("success")
ahmed meraj
  • 644
  • 1
  • 7
  • 13
0
def checkDuplicate():
    count = {}
    for item in lst:
            if item not in count:
                count[item] = 1
            else:
                return True
    return False

  • 4
    While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Brian Apr 09 '20 at 01:05