Nevermore
Nevermore

Reputation: 7399

Check if multiple variables have the same value

I have a set of three variables x, y, z and I want to check if they all share the same value. In my case, the value will either be 1 or 0, but I only need to know if they are all the same. Currently I'm using

if 1 == x and  1 == y and 1 == z: 
    sameness = True

Looking for the answer I've found:

if 1 in {x, y, z}:

However, this operates as

if 1 == x or  1 == y or 1 == z: 
    atleastOneMatch = True

Is it possible to check if 1 is in each: x, y, and z? Better yet, is there a more concise way of checking simply if x, y, and z are the same value?

(If it matters, I use Python 3.)

Upvotes: 40

Views: 87072

Answers (10)

Kelly Bundy
Kelly Bundy

Reputation: 27599

If you had more variables, this way might be most concise, as it only needs an extra , per extra variable:

{x} == {y, z}

(Not as fast as x == y == z, but the question asked for concise, not for fast.)

Upvotes: 0

Simon Pratt
Simon Pratt

Reputation: 357

A couple more interesting ways to solve this, in a more general fashion (because why not?)

def method_one(*args: int) -> bool:
    return max(args) == min(args)

def method_two(*args: int) -> bool:
    return sum(args) in (len(args), 0)

Upvotes: 0

Arpan Saini
Arpan Saini

Reputation: 5191

Below all() and any() functions in python can server the purpose.

all() act as "AND": if all the values in any ittertable object is equal to given condition value, then Return True else return False.

Examples

assert all(b == True for b in [True,True,True,True]) == True
assert all(b == True for b in [False,True,True,True]) == False

any() act as "OR": if any one value in any ittertable object is equal to given condition value, then Return True else return False.

Examples

assert any(b == True for b in [False,False,False,True]) == True
assert any(b == True for b in [False,False,False,False]) == False

Upvotes: 1

cwhisperer
cwhisperer

Reputation: 1926

[x,y,z].count(1)

will count how many variables have 1 as value

Upvotes: 1

zondo
zondo

Reputation: 20336

You could use something similar to what you have:

sameness = (len({x, y, z}) == 1)

This allows for any number of variables. For example:

variables = {x, y, z, a, b, ...}
sameness = (len(variables) == 1)

Note: Creating a set means that each variable needs to be hashed and the hashed values need to be stored, but all() with a generator expression is short-circuiting and keeps track of only two values at a time. Therefore, besides its readability, the generator expression is more efficient.

Upvotes: 2

Martijn Pieters
Martijn Pieters

Reputation: 1121992

If you have an arbitrary sequence, use the all() function with a generator expression:

values = [x, y, z]  # can contain any number of values
if all(v == 1 for v in values):

otherwise, just use == on all three variables:

if x == y == z == 1:

If you only needed to know if they are all the same value (regardless of what value that is), use:

if all(v == values[0] for v in values):

or

if x == y == z:

Upvotes: 84

das-g
das-g

Reputation: 9994

In my case, the value will either by 1 or 2, but I only need to know if they are all the same

Is it possible to check if 1 is in each: x, y, and z? Better yet, is there a more concise way of checking simply if x, y, and z are the same value?

Sure:

x == y == z

which is equivalent to

(x == y) and (y == z)

If you have an arbitrary (nonzero) number of values to compare:

all(values[0] == v for v in values[1:])

Upvotes: 3

Neapolitan
Neapolitan

Reputation: 2153

To check if they are all the same (either 1 or 2):

sameness = (x == y == z)

The parentheses are optional, but I find it improves readability

Upvotes: 8

cuonglm
cuonglm

Reputation: 2816

Another way:

sameness = all(e == 1 for e in [x, y, z])

Upvotes: 1

Vedang Mehta
Vedang Mehta

Reputation: 2254

How about this?

x == y == z == 1

Upvotes: 2

Related Questions