By user1877442

2013-02-27 12:26:23 8 Comments

I'm trying to make a function that will compare multiple variables to an integer and output a string of three letters. I was wondering if there was a way to translate this into Python. So say:

x = 0
y = 1
z = 3
mylist = []

if x or y or z == 0 :
if x or y or z == 1 :
if x or y or z == 2 :
if x or y or z == 3 : 

which would return a list of:

["c", "d", "f"]

Is something like this possible?


@fhgd 2019-11-06 11:11:52


While the pattern for testing multiple values

>>> 2 in {1, 2, 3}
>>> 5 in {1, 2, 3}

is very readable and is working in many situation, there is one pitfall:

>>> 0 in {True, False}

But we want to have

>>> (0 is True) or (0 is False)


One generalization of the previous expression is based on the answer from ytpillai:

>>> any([0 is True, 0 is False])

which can be written as

>>> any(0 is item for item in (True, False))

While this expression returns the right result it is not as readable as the first expression :-(

@Massifox 2019-10-11 12:17:15

Without dict, try this solution:

x, y, z = 0, 1, 3    
offset = ord('c')
[chr(i + offset) for i in (x,y,z)]

and gives:

['c', 'd', 'f']

@Serhii 2019-09-09 18:23:24

You can unite this

x = 0
y = 1
z = 3

in one variable.

In [1]: xyz = (0,1,3,) 
In [2]: mylist = []

Change our conditions as:

In [3]: if 0 in xyz: 
    ...:     mylist.append("c") 
    ...: if 1 in xyz: 
    ...:     mylist.append("d") 
    ...: if 2 in xyz: 
    ...:     mylist.append("e") 
    ...: if 3 in xyz:  
    ...:     mylist.append("f") 


In [21]: mylist                                                                                
Out[21]: ['c', 'd', 'f']

@ThatGuyRussell 2015-08-19 02:34:58

As stated by Martijn Pieters, the correct, and fastest, format is:

if 1 in {x, y, z}:

Using his advice you would now have separate if-statements so that Python will read each statement whether the former were True or False. Such as:

if 0 in {x, y, z}:
if 1 in {x, y, z}:
if 2 in {x, y, z}:

This will work, but if you are comfortable using dictionaries (see what I did there), you can clean this up by making an initial dictionary mapping the numbers to the letters you want, then just using a for-loop:

num_to_letters = {0: "c", 1: "d", 2: "e", 3: "f"}
for number in num_to_letters:
    if number in {x, y, z}:

@Sergei 2019-02-17 17:55:26

Maybe you need direct formula for output bits set.

x=0 or y=0 or z=0   is equivalent to x*y*z = 0

x=1 or y=1 or z=1   is equivalent to (x-1)*(y-1)*(z-1)=0

x=2 or y=2 or z=2   is equivalent to (x-2)*(y-2)*(z-2)=0

Let's map to bits: 'c':1 'd':0xb10 'e':0xb100 'f':0xb1000

Relation of isc (is 'c'):

if xyz=0 then isc=1 else isc=0

Use math if formula

[c]: (xyz=0 and isc=1) or (((xyz=0 and isc=1) or (isc=0)) and (isc=0))

[d]: ((x-1)(y-1)(z-1)=0 and isc=2) or (((xyz=0 and isd=2) or (isc=0)) and (isc=0))


Connect these formulas by following logic:

  • logic and is the sum of squares of equations
  • logic or is the product of equations

and you'll have a total equation express sum and you have total formula of sum

then sum&1 is c, sum&2 is d, sum&4 is e, sum&5 is f

After this you may form predefined array where index of string elements would correspond to ready string.

array[sum] gives you the string.

@Mayur 2019-04-05 22:03:52

This will help you.

def test_fun(val):
    x = 0
    y = 1
    z = 2
    myList = []
    if val in (x, y, z) and val == 0:
    if val in (x, y, z) and val == 1:
    if val in (x, y, z) and val == 2:


@Siddharth Satpathy 2018-12-03 05:13:18

You can try the method shown below. In this method, you will have the freedom to specify/input the number of variables that you wish to enter.

mydict = {0:"c", 1:"d", 2:"e", 3:"f"}
mylist= []

num_var = int(raw_input("How many variables? ")) #Enter 3 when asked for input.

for i in range(num_var): 
    ''' Enter 0 as first input, 1 as second input and 3 as third input.'''
    globals()['var'+str('i').zfill(3)] = int(raw_input("Enter an integer between 0 and 3 "))
    mylist += mydict[globals()['var'+str('i').zfill(3)]]

print mylist
>>> ['c', 'd', 'f']

@Seenivasan 2018-11-06 14:26:24

It can be done easily as

for value in [var1,var2,var3]:

@alamin 2018-09-20 02:18:55

To test multiple variables with one single value: if 1 in {a,b,c}:

To test multiple values with one variable: if a in {1, 2, 3}:

@Rolf of Saxony 2016-09-10 15:44:32

All of the excellent answers provided here concentrate on the specific requirement of the original poster and concentrate on the if 1 in {x,y,z} solution put forward by Martijn Pieters.
What they ignore is the broader implication of the question:
How do I test one variable against multiple values?
The solution provided will not work for partial hits if using strings for example:
Test if the string "Wild" is in multiple values

>>> x = "Wild things"
>>> y = "throttle it back"
>>> z = "in the beginning"
>>> if "Wild" in {x, y, z}: print (True)


>>> x = "Wild things"
>>> y = "throttle it back"
>>> z = "in the beginning"
>>> if "Wild" in [x, y, z]: print (True)

for this scenario it's easiest to convert to a string

>>> [x, y, z]
['Wild things', 'throttle it back', 'in the beginning']
>>> {x, y, z}
{'in the beginning', 'throttle it back', 'Wild things'}

>>> if "Wild" in str([x, y, z]): print (True)
>>> if "Wild" in str({x, y, z}): print (True)

It should be noted however, as mentioned by @codeforester, that word boundries are lost with this method, as in:

>>> x=['Wild things', 'throttle it back', 'in the beginning']
>>> if "rot" in str(x): print(True)

the 3 letters rot do exist in combination in the list but not as an individual word. Testing for " rot " would fail but if one of the list items were "rot in hell", that would fail as well.
The upshot being, be careful with your search criteria if using this method and be aware that it does have this limitation.

@hamid 2015-04-10 03:29:39

If you want to use if, else statements following is another solution:

myList = []
aList = [0, 1, 3]

for l in aList:
    if l==0: myList.append('c')
    elif l==1: myList.append('d')
    elif l==2: myList.append('e')
    elif l==3: myList.append('f')


@firelynx 2018-08-27 09:45:00

Looks like you're building some kind of Caesar cipher.

A much more generalized approach is this:

input_values = (0, 1, 3)
origo = ord('c')
[chr(val + origo) for val in inputs]


['c', 'd', 'f']

Not sure if it's a desired side effect of your code, but the order of your output will always be sorted.

If this is what you want, the final line can be changed to:

sorted([chr(val + origo) for val in inputs])

@rsalmei 2018-08-06 06:26:34

The most mnemonic way of representing your pseudo-code in Python would be:

x = 0
y = 1
z = 3
mylist = []

if any(v == 0 for v in (x, y, z)):
if any(v == 1 for v in (x, y, z)):
if any(v == 2 for v in (x, y, z)):
if any(v == 3 for v in (x, y, z)):

@imposeren 2019-05-04 04:47:11

This approach is more universal than ` if 2 in (x, y, z): mylist.append('e')` because allows arbitrary comparisons (e.g. if any(v >= 42 for v in (x, y, z)): ). And performance of all 3 methods (2 in {x,y,z}, 2 in (x,y,z), any(_v == 2 for _v in (x,y,z))) seems to be almost the same in CPython3.6 (see Gist)

@Rohit Gawas 2018-07-31 16:54:00

You can use dictionary :

x = 0
y = 1
z = 3
dict = {0: 'c', 1: 'd', 2: 'e', 3: 'f'}
if x in dict:

if y in dict:
if z in dict:

print list

@Sergei 2019-02-19 04:49:25

This may append same more then once this. Set?

@B. M. 2015-06-09 20:46:34

Set is the good approach here, because it orders the variables, what seems to be your goal here. {z,y,x} is {0,1,3} whatever the order of the parameters.

>>> ["cdef"[i] for i in {z,x,y}]
['c', 'd', 'f']

This way, the whole solution is O(n).

@Raniz 2015-06-10 04:19:22

You should add a description of what your code accomplishes and how it does it. Short answers using only code is discouraged

@Martijn Pieters 2013-02-27 12:27:40

You misunderstand how boolean expressions work; they don't work like an English sentence and guess that you are talking about the same comparison for all names here. You are looking for:

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

x and y are otherwise evaluated on their own (False if 0, True otherwise).

You can shorten that using a containment test against a tuple:

if 1 in (x, y, z):

or better still:

if 1 in {x, y, z}:

using a set to take advantage of the constant-cost membership test (in takes a fixed amount of time whatever the left-hand operand is).

When you use or, python sees each side of the operator as separate expressions. The expression x or y == 1 is treated as first a boolean test for x, then if that is False, the expression y == 1 is tested.

This is due to operator precedence. The or operator has a lower precedence than the == test, so the latter is evaluated first.

However, even if this were not the case, and the expression x or y or z == 1 was actually interpreted as (x or y or z) == 1 instead, this would still not do what you expect it to do.

x or y or z would evaluate to the first argument that is 'truthy', e.g. not False, numeric 0 or empty (see boolean expressions for details on what Python considers false in a boolean context).

So for the values x = 2; y = 1; z = 0, x or y or z would resolve to 2, because that is the first true-like value in the arguments. Then 2 == 1 would be False, even though y == 1 would be True.

The same would apply to the inverse; testing multiple values against a single variable; x == 1 or 2 or 3 would fail for the same reasons. Use x == 1 or x == 2 or x == 3 or x in {1, 2, 3}.

@SingleNegationElimination 2013-10-24 15:27:12

I wouldn't be so quick to go for the set version. Tuple's are very cheap to create and iterate over. On my machine at least, tuples are faster than sets so long as the size of the tuple is around 4-8 elements. If you have to scan more than that, use a set, but if you are looking for an item out of 2-4 possibilities, a tuple is still faster! If you can arrange for the most likely case to be first in the tuple, the win is even bigger: (my test: timeit.timeit('0 in {seq}'.format(seq=tuple(range(9, -1, -1)))))

@Martijn Pieters 2013-10-24 15:29:51

@dequestarmappartialsetattr: In Python 3.3 and up, the set is stored as a constant, bypassing the creation time altogether, eliminating the creation time. Tuples can be cheap to create as Python caches a bundle of them to avoid memory churn, making that the biggest difference with sets here.

@Martijn Pieters 2013-10-24 15:37:11

@dequestarmappartialsetattr: If you time just the membership test, for integers sets and tuples are equally fast for the ideal scenario; matching the first element. After that tuples lose out to sets.

@ShadowRanger 2016-09-04 00:37:09

@MartijnPieters: Using the set literal notation for this test isn't a savings unless the contents of the set literal are also literals, right? if 1 in {x, y, z}: can't cache the set, because x, y and z could change, so either solution needs to build a tuple or set from scratch, and I suspect whatever lookup savings you might get when checking for membership would be swamped by greater set creation time.

@Martijn Pieters 2016-09-04 07:58:07

@ShadowRanger: yes, peephole optimisation (be it for in [...] or in {...}) only works if the contents of the list or set are immutable literals too.

@oldboy 2018-07-08 21:30:02

out of curiosity, i have a list of URLs as strings whereby i'm checking if the string contains multiple substrings (i.e. 'word1' in string and 'word2' in string...). would the same syntax (i.e. string in (word1, word2):) work in this scenario or, as i am expecting, would it fail?

@Martijn Pieters 2018-07-09 11:27:58

@Anthony: that's the wrong test; string is not contained in the sequence (word1, word2). You can use all(w in string for w in sequence_of_words_to_test) if you have multiple words to test in one string. Or use the word1 in string and word2 in string approach if you have just 2 or 3 such strings to test, and they are hardcoded.

@Bernhard Barker 2018-09-24 12:56:13

in is not constant-time "whatever the left-hand operand is" - technically with set you'd require at most the time taken to return the hash (calculate it if you didn't cache it) and do an equals check, which can very well take longer than constant time. Although this cost also exists with tuples.

@Martijn Pieters 2018-09-24 13:12:49

@Dukeling: 'constant time' is not a fixed quantity. It means the algorithmic complexity is O(1); it doesn't matter how many elements are in the set, containment testing will take the same amount of time, roughly, independently of the size of the set.

@Martijn Pieters 2018-09-24 13:14:33

@Dukeling: containment testing against a list or tuple or other sequence takes linear time, O(N), as each element is tested one by one. So when the tuple or list or sequence doubles, so does your average time taken. Sets and dictionaries thus beat sequences, especially as the number of elements grows.

@Martijn Pieters 2018-09-24 13:15:31

@Dukeling: calculating the hash is a separate O(something) operation, which is independent of the N size of the set, dict, tuple, etc. on the right-hand side. Python immutable types cache the value anyway.

@Bernhard Barker 2018-09-24 13:18:12

@MartijnPieters I know, but I'm still not sure why you say "whatever the left-hand operand is", because the cost changes depending on which elements you have in the set (even if it's always pseudo-constant) - I'd be fine with that paragraph if you remove that part, or replace it with "regardless of how many elements you have in the set*".

@Martijn Pieters 2018-09-24 13:50:40

@Dukeling: I really don't see a need. Bad hash implementations are the exception, not the norm. We'd have to qualify listobj[index] as O(1) (provided index is not a custom Python object with a bad __index__ implementation), or sorting a list as *O(N log N) (provided the contents do not have complex ordering implementations that throw off comparison costs). For contains operations, the cost can be assumed to be constant. If the hash cost for a 'bad actor' object is large, it'll still be the same cost independent of the size of the set you test against.

@cs95 2018-12-27 09:19:02

@SingleNegationElimination sure, tuples are faster than sets when comparing 2-3 values, but I would rank an answer imparting good practices from a time complexity perspective much higher than an answer that takes advantage of implementation details (which are subject to change) to achieve microptimizations on the level of "seriously, don't worry about it".

@user11093202 2019-04-06 21:55:58

This is a quite simple thing. Just add it for each section, the variable you are testing. I read the things, and in my opinion, they have no relation to the question whatsoever.

@smci 2019-04-10 10:17:09

a) The set approach is non-obvious, and there's always the generic pitfall with "falsey" non-integer values: False in (1,0,2) and 0.0 in (1,0,2) both give True. b) Aliasing: accidental use of if x is y with small integers and interned strings can also give the wrong answer. Are this question and its answers only for integers? c) in is overloaded for both strings and sequences; consider: '77' in '777' is True, but '77' in ('666','777') is False. d) ...

@smci 2019-04-10 10:20:55

d) Subclasses/superclasses: dict() == collections.Counter() is True. e) Iterators and ranges: [0,1,2] == range(3) is False. Although [0,1,2] == list(range(3)) is True

@Martijn Pieters 2019-04-10 18:33:20

@smci: I don't know what you are getting at here. You are describing normal equality testing rules, sets don't alter those. False == 0 is also True, and so is 0.0 == 0. And at least with a set membership test you avoid the sometimes-made is error. I'm not saying anything about using in on strings or tuples.

@aerijman 2019-08-22 13:54:12

I even found if 1 in (x, y, z) slightly faster than if x == 1 or y == 1 or z == 1

@dansalmo 2013-07-11 21:56:54

Your problem is more easily addressed with a dictionary structure like:

x = 0
y = 1
z = 3
d = {0: 'c', 1:'d', 2:'e', 3:'f'}
mylist = [d[k] for k in [x, y, z]]

@aragaer 2013-10-24 15:39:35

Or even d = "cdef" which leads to MyList = ["cdef"[k] for k in [x, y, z]]

@dansalmo 2014-05-08 14:36:49

or map(lambda i: 'cdef'[i], [x, y, z])

@dansalmo 2018-07-24 21:05:58

@MJM the output order is not determined by the dict, it is determined by the order of the list [x, y, z]

@LoneWanderer 2019-03-10 18:57:53

Aside from the list comprehension which I'm not yet fully accustomed to, most of us had the same reflex: build that dict !

@Vinayak Kaniyarakkal 2017-06-05 06:50:28

One line solution:

mylist = [{0: 'c', 1: 'd', 2: 'e', 3: 'f'}[i] for i in [0, 1, 2, 3] if i in (x, y, z)]


mylist = ['cdef'[i] for i in range(4) if i in (x, y, z)]

@ytpillai 2015-05-25 03:53:36

If you ARE very very lazy, you can put the values inside an array. Such as

list = []
nums = [add numbers here]
letters = [add corresponding letters here]
for index in range(len(nums)):
    for obj in list:
        if obj == num[index]:

You can also put the numbers and letters in a dictionary and do it, but this is probably a LOT more complicated than simply if statements. That's what you get for trying to be extra lazy :)

One more thing, your

if x or y or z == 0:

will compile, but not in the way you want it to. When you simply put a variable in an if statement (example)

if b

the program will check if the variable is not null. Another way to write the above statement (which makes more sense) is

if bool(b)

Bool is an inbuilt function in python which basically does the command of verifying a boolean statement (If you don't know what that is, it is what you are trying to make in your if statement right now :))

Another lazy way I found is :

if any([x==0, y==0, z==0])

@wjandrea 2020-02-08 05:36:16

-1 There's a lot of bad practice here. list is a Python builtin; use another name instead, like xyz for example. Why do you construct the list in four steps when you can do one, i.e. xyz = [x, y, z]? Don't use parallel lists, use a dict instead. All in all, this solution is much more convoluted than ThatGuyRussell's. Also for the last part, why not do a comprehension, i.e. any(v == 0 for v in (x, y, z))? Also arrays are something else in Python.

@michael zxc858 2015-06-29 07:03:58

This code may be helpful

L ={x, y, z}
T= ((0,"c"),(1,"d"),(2,"e"),(3,"f"),)
for t in T :
if t[0] in L :

@Saksham Varma 2015-02-27 01:31:00

d = {0:'c', 1:'d', 2:'e', 3: 'f'}
x, y, z = (0, 1, 3)
print [v for (k,v) in d.items() if x==k or y==k or z==k]

@Bhargav Boda 2015-01-13 12:10:25

I think this will handle it better:

my_dict = {0: "c", 1: "d", 2: "e", 3: "f"}

def validate(x, y, z):
    for ele in [x, y, z]:
        if ele in my_dict.keys():
            return my_dict[ele]


print validate(0, 8, 9)
print validate(9, 8, 9)
print validate(9, 8, 2)

@GuiltyDolphin 2014-06-04 17:09:22

To check if a value is contained within a set of variables you can use the inbuilt modules itertools and operator.

For example:


from itertools import repeat
from operator import contains

Declare variables:

x = 0
y = 1
z = 3

Create mapping of values (in the order you want to check):

check_values = (0, 1, 3)

Use itertools to allow repetition of the variables:

check_vars = repeat((x, y, z))

Finally, use the map function to create an iterator:

checker = map(contains, check_vars, check_values)

Then, when checking for the values (in the original order), use next():

if next(checker)  # Checks for 0
    # Do something
elif next(checker)  # Checks for 1
    # Do something


This has an advantage over the lambda x: x in (variables) because operator is an inbuilt module and is faster and more efficient than using lambda which has to create a custom in-place function.

Another option for checking if there is a non-zero (or False) value in a list:

not (x and y and z)


not all((x, y, z))

@wallacer 2014-06-04 17:39:09

This doesn't answer the OP's question. It only covers the first case in the provided example.

@akaRem 2013-07-11 21:16:33

The direct way to write x or y or z == 0 is

if any(map((lambda value: value == 0), (x,y,z))):
    pass # write your logic.

But I dont think, you like it. :) And this way is ugly.

The other way (a better) is:

0 in (x, y, z)

BTW lots of ifs could be written as something like this

my_cases = {
    0: Mylist.append("c"),
    1: Mylist.append("d")
    # ..

for key in my_cases:
    if key in (x,y,z):

@SethMMorton 2014-02-08 20:57:33

In your example of the dict instead of a key, you will get errors because the return value of .append is None, and calling None gives an AttributeError. In general I agree with this method, though.

@Mahmoud Elshahat 2019-04-07 01:41:32

the dict instead of a key is wrong, you will get Mylist=['c', 'd'] when the dictionary get initialized even if you commented out "for..loop" part

@Alex 2019-09-16 05:17:22

In your first example filter would be better than map, as it will return only instances where lambda evaluates to true

@wjandrea 2020-02-08 05:21:05

A comprehension is much simpler than a map of a lambda: any(v == 0 for v in (x, y, z))

Related Questions

Sponsored Content

8 Answered Questions

[SOLVED] How to return dictionary keys as a list in Python?

26 Answered Questions

[SOLVED] Pythonic way to create a long multi-line string

11 Answered Questions

[SOLVED] How to access environment variable values?

10 Answered Questions

[SOLVED] Why is "1000000000000000 in range(1000000000000001)" so fast in Python 3?

34 Answered Questions

[SOLVED] How do I sort a dictionary by value?

15 Answered Questions

[SOLVED] How does Python's super() work with multiple inheritance?

17 Answered Questions

[SOLVED] How to make a chain of function decorators?

32 Answered Questions

[SOLVED] "Least Astonishment" and the Mutable Default Argument

12 Answered Questions

[SOLVED] Calling a function of a module by using its name (a string)

  • 2008-08-06 03:36:08
  • ricree
  • 700305 View
  • 1730 Score
  • 12 Answer
  • Tags:   python object

Sponsored Content