hexdec123
hexdec123

Reputation: 45

How to check if a list contains only boolean value

I want to write a function:

def CheckBoolean(lst):

if the list only contains True or False, it will return True, otherwise it will return False

Upvotes: 0

Views: 1092

Answers (2)

Vishal Singh
Vishal Singh

Reputation: 6234

You can use Python built-in function all for this.

result = all(isinstance(item, bool) for item in last)

all return True if all elements of the iterable are true (or if the iterable is empty).

Upvotes: 2

Rushil S
Rushil S

Reputation: 88

def CheckBoolean(lst):
    return all(isinstance(i, bool) for i in lst)

Upvotes: 1

Related Questions