Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check at once the boolean values from a set of variables

Tags:

python

boolean

I am having around 10 boolean variables, I need to set a new boolean variable x=True if all those ten variable values are True.If one of them is False then set x= False I can do this in a manner

if (a and b and c and d and e and f...):
    x = True
else:
    x=False

which obviously looks very ugly.Please suggest some more pythonic solutions.

The ugly part is a and b and c and d and e and f...

like image 554
NIlesh Sharma Avatar asked Dec 21 '22 17:12

NIlesh Sharma


1 Answers

Assuming you have the bools in a list/tuple:

x = all(list_of_bools)

or just as suggested by @minopret

x= all((a, b, c, d, e, f))

example:

>>> list_of_bools = [True, True, True, False]
>>> all(list_of_bools)
False
>>> list_of_bools = [True, True, True, True]
>>> all(list_of_bools)
True
like image 74
jamylak Avatar answered Jan 16 '23 16:01

jamylak