-1

I created a function to evaluate list if they have duplicates or not:

def duplica(list_to_check):
    if len(set(list_to_check)) != len(list_to_check):
        print('there are duplicates inside the list')
        result = 0  
    else:
        result = 1
    
    return result

print(duplica([1, 1, 2]))

##test it:
there are duplicates inside the list 
0

I want to know if there's any alternative way to evaluate the list using a code of only one line (for example lambda or map)

max scender
  • 113
  • 6

1 Answers1

1

If you only need the value:

0 if len(set(list_to_check)) != len(list_to_check) else 1

or even better (): (provided by: Olvin Roght in the comment)

int(len(set(list_to_check)) == len(list_to_check))

With print:

(0,print('there are duplicates inside the list'))[0] if len(set(list_to_check)) != len(list_to_check) else 1
Andreas
  • 8,694
  • 3
  • 14
  • 38
  • Bool can be used in operations as integer normally, so you don't even need `int()`. – Lab Aug 09 '20 at 14:00