34

I am using Python and I would like to have an if statement with many variables in it.

Such as:

if A, B, C, and D >= 2:
    print (A, B, C, and D)

I realize that this is not the correct syntax and that is exactly the question I am asking — what is the correct Python syntax for this type of an if statement?

1

7 Answers 7

53

You want to test a condition for all the variables:

if all(x >= 2 for x in (A, B, C, D)):
    print(A, B, C, D)

This should be helpful if you're testing a long list of variables with the same condition.


If you needed to check:

if A, B, C, or D >= 2:

Then you want to test a condition for any of the variables:

if any(x >= 2 for x in (A, B, C, D)):
    print(A, B, C, D)
Sign up to request clarification or add additional context in comments.

Comments

20

Another idea:

if min(A, B, C, D) >= 2:
    print A, B, C, D

8 Comments

It works for this particular case, but in general, you can't compare the same condition for multiple variables at once
min means: find the minimum of all the numbers between the parenthesis. If even the minimum is greater or equal than 2, we can conclude that all of the variables are greater or equal than 2
Oh I get it now! I know what Limbo Peng was saying now! Thanks for the help!
min(...) won't shortcircuit whereas all(...) does
@bendl: It stops evaluating as soon as the result is known.
|
7

I'd probably write this as

v = A, B, C, D
if all(i >= 2 for i in v):
    print v

5 Comments

Thanks! To clarify the meaning of what you are telling me... If I had A, B, C, and D. Could I just say v = A, B, C, D (RETURN - can't get the code tag to work) if all(v >= 2): (RETURN) print (v)?
@TerriMoore: No, because that would be asking if the tuple (A, B, C, D) was greater than or equal to 2, not whether all the values are >= 2. For technical reasons that won't actually raise an Exception in python 2, it'll just give unexpected results, but it will raise an error (as it should) in python 3.
I am using python 3. What exactly would the "i in v" thing be looking at?
@LimboPeng's answer is a little more directly implemented, without the intermediate 'v' variable.
OK so you recommend Limbo's answer over DSM's?
3

If you have ten variables that you are treating as a group like this, you probably want to make them elements of a list, or values in a dictionary, or attributes of an object. For example:

my_dict = {'A': 1, 'B': 2, 'C': 3 }

if all(x > 2 for x in my_dict.values()):
    print "They're all more than two!"

1 Comment

No that isn't what I am trying to achieve here. But thanks anyways!
3

How about:

if A >= 2 and B >= 2 and C >= 2 and D >= 2:
    print A, B, C, D

For the general case, there isn't a shorter way for indicating that the same condition must be true for all variables - unless you're willing to put the variables in a list, for that take a look at some of the other answers.

9 Comments

Thanks! I have 10 variables is their anything that takes less typing?
If you need to test the same condition (>= 2) and all the variables are numbers, @Fred Larson's answer is the shortest solution (albeit a bit hard to understand at a glance)
If I need to do other conditions would the answer provided by DSM be the best?
Same thing: it works only if the condition is the same for all variables; otherwise you have to test each variable individually, one by one, as in my answer
Ya I want them to all be tested against the same condition individually not as a group but I want to put it all into one if statement.
|
2

Depending on what you are trying to accomplish, passing a list to a function may work.

def foo(lst):
    for i in lst:
        if i < 2:
            return
    print lst

3 Comments

Oops! I've just posted the same answer as yours. LOL
If you could explain what this is doing in more detail I would really help me. Thanks!
You pass the function a list of numbers. For each item in the list, check to see if the item is less than 2 - if the item is less than 2, then the function "returns" (or exits) without printing anything. Continue checking each item in the list. Once each item has been checked, and no item caused the function to return/exit, we know that ALL items are >= 2, so we can print the list.
1

Except that she's probably asking for this:

if A >= 2 and B >= 2 and C >= 2 and D >= 2:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.