i want use function find duplicate items in list, code not working:
p = "enter list\n" t = raw_input(p) def has_duplicate(t): o = sorted(t) = 0 while < len(o): if o[i] == o[i + 1]: print "the list has duplicates" elif o[i] != o[i+1]: += 1 if >= len(o): print "the list has no duplicate"
it gives me error saying has_duplicates
not defined.
as @mgilson commented, issue calling function incorrectly (has_duplicates
vs has_duplicate
) however...
the straight forward way using set
, comparing len
.
def has_duplicates(t): return len(set(t)) != len(t)
if take iterable , wrap in set
end unique items. if length of set same original iterable have no duplicates. if length different (will equal or smaller) have duplicates removed when converting set
type.
Comments
Post a Comment