0

In a conditional like if (bool) and (bool):, what is the cleanest expression that will return True if both evaluations are False?

I see many options to logically make this work, but they all look messy to me.

øøø
  • 1
  • 3
  • 1
    Did you read https://stackoverflow.com/questions/16522111/python-syntax-for-if-a-or-b-or-c-but-not-all-of-them ? It seems to have good examples – B. Go Sep 23 '19 at 00:04
  • if (not bool1) and (not bool2) should work, as if not (bool1 or bool2) – B. Go Sep 23 '19 at 00:05
  • Possible duplicate of [Python syntax for "if a or b or c but not all of them"](https://stackoverflow.com/questions/16522111/python-syntax-for-if-a-or-b-or-c-but-not-all-of-them) – MyNameIsCaleb Sep 23 '19 at 00:05
  • @B.Go, yes. Those are the best options I've seen. Thank you. – øøø Sep 23 '19 at 00:24
  • @MyNameIsCaleb, the answer from the link you gave does apply to my question, but the question from that link is not as specific as mine. – øøø Sep 23 '19 at 01:19

1 Answers1

0

You can either use all:

if all(not b for b in [bool_1, bool_2, ..., bool_n]):
# means: if all are not True (False)

any:

if not any([bool_1, bool_2, ..., bool_n]):
# means: if not any of them is True (same as above, logically)

Or, sum (only with bool type, otherwise use sum(map(bool, [bool_1, ..., bool_n]))):

if sum([bool_1, bool_2, ..., bool_n]) == 0:   #  or, if not sum(...):
# True is 1, False is 0, if sum is 0, then all are 0 (False)
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55