-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

2 Answers2

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
  • 7,669
  • 3
  • 9
  • 30
  • Bool can be used in operations as integer normally, so you don't even need `int()`. – Lab Aug 09 '20 at 14:00
-1

You can do:

def check_duplicates(items):
    return len(set(items)) == len(items)
    # return len(items) - len(set(items)) # if you need a number


if __name__ == '__main__':
    test = [11, 21, 3, 11, 2]
    print(f'There are duplicates inside the list:\n{int(check_duplicates(test))}')
sK500
  • 1,289
  • 8
  • 19