Check if multiple variables have the same value

Question:

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.)

Asked By: Nevermore

||

Answers:

How about this?

x == y == z == 1
Answered By: Vedang Mehta

Another way:

sameness = all(e == 1 for e in [x, y, z])
Answered By: cuonglm

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:
Answered By: Martijn Pieters

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

Answered By: Neapolitan

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.

Answered By: zondo

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:])
Answered By: das-g
[x,y,z].count(1)

will count how many variables have 1 as value

Answered By: cwhisperer

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
Answered By: Arpan Saini

If you had more variables, this way might be most concise:

{x} == {y, z}

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

Answered By: Kelly Bundy
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.