Is there a way to write an If
(or equivalent) statement that can have many arguments, and if any of those satisfy the logic, use that variable?
For instance
if len(x) == 1 or len(y) == 1 or len(z) == 1 or ... len(zz) == 1:
# do something with the variable that met the condition
So say only z
has length 1
, could I write above idea/formula in a way that takes the first True
answer and use that?
So something like
x = "123"
y = "234"
z = "2"
xx = "1234"
yy = "12345"
if len(x) == 1 or len(y) == 1 or len(z) == 1 or len(xx) == 1 or len(yy) == 1:
#do something with the variable that satisfies the condition, so `z` in this case.
Does that make any sense? The variables' lengths could change any time, so I'd like to be able to say "If any of the conditions are met, use the variable that met the condition"...?
In the above, I don't know beforehand that z
will be the only one to meet the criteria, so my Then
statement can't be z = "new value"
or whatever I want
to do with it.
Edit: Sorry, per comments I know checking for len
on integers isn't okay. This is solely for illustration purposes and it was the first thing I thought of to "test". Sorry if the len
bit is confusing. I'm mainly just trying to see if I can use If
statements (or related ones) where I don't know which of my many variables will meet a condition. (I'm still new regarding python, so my sincere apologies for my lack of semantics or proper terms). I'd like to avoid elif
if at all possible just because it can get stringy. (But if that's the most pythonic way, then so be it!)