Ibrahim Rahimi
Ibrahim Rahimi

Reputation: 539

Convert a loop to a lambda in python 3 to return true or false with out using filter method?

I wrote following code to get True of False if an element of a list is duplicate in it. I want to convert this code to a lambda version to reduce code length. Is there anyway to do it without using filter() method?

def isoscelesTriangle(s):
    for i in range(len(s)):
        if s.count(s[i]) > 1:
            return True
    return False 

Upvotes: 1

Views: 162

Answers (1)

Austin
Austin

Reputation: 26057

This uses lambda:

def isoscelesTriangle(s):
    return any(map(lambda x: s.count(x) > 1, s))

But, seriously you only need below one. This requires you to only compare length of list with length of it's set:

def isoscelesTriangle(s):
    return len(s) != len(set(s))

Upvotes: 2

Related Questions