1

So I have a list called puzzle which contains the follow lists:

puzzle = [[1, 3, 5, 5, 4],
[3, 5, 1, 3, 4],
[2, 3, 4, 5, 1],
[1, 5, 3, 2, 2],
[5, 4, 1, 3, 2]]

I would like to check each list inside puzzle and test if there are any duplicate numbers that are not zero, in which case the code would return false. How can I do this?

1 Answers1

0

Almost the same approach -- except that you'd run it on a sub-list without zeros in it.

def has_dup(lst):
    no_zeros = [x for x in lst if x != 0]
    return len(set(no_zeros)) != len(no_zeros)

is_valid = any(has_dup(x) for x in puzzle)
UltraInstinct
  • 43,308
  • 12
  • 81
  • 104