I like to use any()
and all()
in Python. But sometimes I need a one()
.
I want to know if only one value or condition in a list is True
.
Currently I do it with such a nested ugly if
.
# In real world there could be more then just two elements
istrue = [True, False]
isfalse = [True, 'foobar']
isfalse2 = [False, False]
def one(values):
if values[0] and values[1]:
return False
if not values[0] and not values[1]:
return False
return True
one(istrue) # True
one(isfalse) # False
one(isfalse2) # False
I assume there is a more pythonic way, isn't it?