Python Conditional Variable Setting
Asked Answered
S

6

59

For some reason I can't remember how to do this - I believe there was a way to set a variable in Python, if a condition was true? What I mean is this:

 value = 'Test' if 1 == 1

Where it would hopefully set value to 'Test' if the condition (1 == 1) is true. And with that, I was going to test for multiple conditions to set different variables, like this:

 value = ('test' if 1 == 1, 'testtwo' if 2 == 2)

And so on for just a few conditions. Is this possible?

Sorrells answered 14/11, 2011 at 0:51 Comment(1)
it's called a Ternary OperatorReckford
G
122

This is the closest thing to what you are looking for:

value = 'Test' if 1 == 1 else 'NoTest'

Otherwise, there isn't much else.

Ghostly answered 14/11, 2011 at 0:53 Comment(3)
Ah, I was missing the 'else', and it didn't tell me I needed one. I guess I should've known better, since if the condition doesn't equate, it wouldn't be set to anything. Thanks!Sorrells
In case it isn't obvious, you can chain these (as in value = v1 if c1 else v2 if c2 else v3). An alternative is to use a dictionary.Olympic
Tip: The use of 'else' is mandatory here. But if the variable already exists, and you don't want it to change if the condition is not satisfied, just make it equal to itself: value = 'Test' if 1 == 1 else valueFournier
O
8

You can also do:

value = (1 == 1 and 'test') or (2 == 2 and 'testtwo') or 'nope!'

I prefer this way :D

Oedema answered 1/9, 2018 at 9:29 Comment(1)
It looks good, but this will not work as we want: value = (1 == 1 and '') or (2 == 2 and 'testtwo') or 'nope!'Zacharia
E
3

Less obvious but nice looking term:

value = ('No Test', 'Test')[1 == 1]
print(value) # prints 'Test'
Emmettemmey answered 5/10, 2019 at 7:16 Comment(2)
Let's please have it obvious. We code for humans. Not for machines. The machines can read anything that's not an error, alright. But humans cannot. And it's us that make the mistakes so we have to make it easier for us. :)Sympathy
I actually like this a lot. It's quite understandable (and I happen to be a human). Thanks for sharing this extra tidbit. Cheers!Gombosi
I
1

value = [1, 2][1 == 1] ;)

...well I guess this would work too: value = ['none true', 'one true', 'both true'][(1 == 1) + (2 == 2)]

Not exactly good programming practice or readable code but amusing and compact, at the very least. Python treats booleans as numbers, so True is 1 and False is 0. [1, 2][True] = 2, [1, 2][False] = 1 and [1, 2, 3][True + True] = 3

Indention answered 25/6, 2019 at 6:52 Comment(0)
H
0

Multiple if conditions can be used to assign value like switch case:

>>> inp=2
>>> res='two' if inp==2 else 'one' if inp==1 else 'three' if inp==3 else 'invalid'
>>> res
>>> two
Highwayman answered 1/6, 2023 at 22:34 Comment(0)
O
0

(value:='Test') if 1==1 else ()

Overlay answered 30/1 at 5:50 Comment(2)
Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Burnard
Please explain the functionally relevant difference to the very similar looking solution in the old and upvoted existing answer https://mcmap.net/q/327590/-python-conditional-variable-setting . Explain what difference your proposal makes, what the advantage is. Also please note that explained answers are usually better received than code only answer. Take the tour read How to Answer and maybe enjoy stackoverflow.com/help/formattingDispersant

© 2022 - 2024 — McMap. All rights reserved.