mlissner
mlissner

Reputation: 18166

Better way to check multiple variables equal a single value in Python

In Python I want to check the following:

if x is None and y is None and z is None and ...

I'd rather say something like:

if x, y, z is None:

But that's not valid syntax. Is there a better way to do this?

Upvotes: 1

Views: 108

Answers (2)

Jace Browning
Jace Browning

Reputation: 12662

If you only care about falsy-ness (which is often the case):

if not all((x, y, z)): ...

Upvotes: 0

Alan
Alan

Reputation: 9620

You want to make sure all expressions satisfy a condition. The Python builtin all is made precisely for that. Since we have no need to introduce a new variable name, here I'll use the name _, which is a valid Python name and is the convention for a "throw-away" variable.

all(_ is None for _ in (x,y,z))

Upvotes: 4

Related Questions