Hemant Sah
Hemant Sah

Reputation: 417

Pythonic way for checking equality in AND condition

I have a piece of code which looks like below:

a = None
b = None
c = None
d = None

if a==None and b==None and c==None and d==None:
    print("All are None")
else:
    print("Someone is not None")

What would be the pythonic way or shorter way to achieve this as I have more number of variables to check like this?

Upvotes: 1

Views: 79

Answers (3)

blhsing
blhsing

Reputation: 106445

You can chain the comparisons:

if a is b is c is d is None:
    print("All are None")
else:
    print("Someone is not None")

Upvotes: 2

QuantumMecha
QuantumMecha

Reputation: 1541

You can use a list, and check all items in the list using list comprehension:

l_list = [None, None, None, None, None]
if all(item is None for item in l_list):
    print("All items are None")

Upvotes: 5

shahkalpesh
shahkalpesh

Reputation: 33476

if not all((a, b, c, d)):

or

if not any((a, b, c, d)): if you want to check if any of the item is not None

Does this help?

Upvotes: -1

Related Questions