1

I am trying to create an if else code where there is about 20conditions for the elif, how do I create a list of conditions where i can just type something such as:

uno= <9
lol= >20
crad= =<3
list={uno,lol,crad}
if 13=list:
 print("yay")
elif 13!=list:
 print("nay")

That's my current code It should print "yay", instead there is syntax error

PM 2Ring
55.6k6 gold badges96 silver badges202 bronze badges
asked Dec 7, 2017 at 8:21
2
  • 2
    The first 3 lines makes no sense. 9 is greater than what? Anyway, you can create a list of booleans and use any or all function to check them all. Commented Dec 7, 2017 at 8:31
  • Why should that pseudo-code print "yay"? You need to give us a better explanation of what you're trying to do, showing us a bunch of invalid code isn't very useful. It would help if you post the equivalent if..elif...else code, and then we can show you a more compact way of doing it (if there is one). Commented Dec 7, 2017 at 8:42

3 Answers 3

7

It's not actually simpler than writing a chain of if/elif/elif etc, but something like this seems to do what you are asking:

predicates = [lambda x: x<9, lambda x: x>20, lambda x: x<=3]
if all(y(13) for y in predicates):
 print("yay")
else:
 print("nay")

Each predicate is a small anonymous function (a lambda) which receives a single argument and evaluates to either True or False. If you have a large number of arguments you want to check against a large set of predicates, it's nice to be able to encapsulate the predicates like this. The ability to programmatically add or remove predicates from the list truly extends the versatility of this construct beyond what you can (easily, naturally) do with if/elif/elif.

This particular set of predicates can't all be true for a single number. Maybe you want any() instead of all()...?

answered Dec 7, 2017 at 8:55
Sign up to request clarification or add additional context in comments.

Comments

3

Your "conditions" are functions mapping the input to booleans. Therefore, you can write them as functions:

def is_small(number):
 return number < 9
def is_large(number):
 return number > 20
conditions = (is_small, is_large)

Then you can evaluate all of these functions on some input:

def check_all(input, conditions):
 return [condition(input) for condition in conditions]
check_all(10, conditions)
>>> [False, False]

And if you want to know if all of these or any one of these are true, you can use the functions all and any:

any(check_all(10, conditions))
>>> False
any(check_all(21, conditions))
>>> True

And not all(...) is True if one of the conditions is not fulfilled, not any is True if none is.

Edit: One thing to notice is that the list comprehension [... for ... in ...] in check_all always evaluates all functions. This is not necessary if you use any or all, which can use an iterator and stop evaluating it onces the result it fixed (at the first True for any and the first False for all). If you use them, you can just replace the list comprehension [... for ... in ...] by a generator expression (... for ... in ...).

answered Dec 7, 2017 at 8:52

2 Comments

One of the nice features of any is that it won't evaluate the rest of the list as soon as it finds an item which returns True. It would be nice if you could use this shortcut logic, so I would remove the check_all() wrapper, or refactor it to use any() or all() depending on how you use it.
The function does not hurt, especially not readability. But using a generator expression there does not hurt either. Thanks.
1

You could create a tuple of booleans and check it via all.

conditions = []
conditions.append(yourVar < 9)
conditions.append(yourVar > 20)
conditions.append(yourVar <= 3)
if all(conditions):
 print('All conditions are met')
else:
 print('At least one condition results in false)
answered Dec 7, 2017 at 8:52

2 Comments

This evaluates yourVar at the time of append. I'm imagining the OP would like something which can be applied dynamically to a collection of values.
Yes if he needs a dynamic evaluation then your solution is definitely better. What he needs is not totally clear.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.