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!)
The Excel OR function returns TRUE if any given argument evaluates to TRUE, and returns FALSE if all supplied arguments evaluate to FALSE. For example, to test A1 for either "x" or "y", use =OR(A1="x",A1="y").
IF Function is one of the most powerful functions in excel. And, the best part is, you can combine other functions with IF to increase its power. Combining IF and OR functions is one of the most useful formula combinations in excel.
While Excel will allow you to nest up to 64 different IF functions, it's not at all advisable to do so.
While @pault 's answer addresses your question, I think it isn't super readable. If you have a couple of variables only, pythons mantra dictate a straightforward, explicit way:
if len(x) == 1:
f(x)
elif len(y) == 1:
f(y)
elif len(z) == 1:
f(z)
Otherwise, if you have a list, a for loop is readable and efficient:
for l in ls:
if len(l) == 1:
f(l)
break
You could use next
here to pick the first item out of a list of options that meets your criteria:
value = next((item for item in [x, y, z] if len(item)==1), None)
if value is not None:
...
The second argument to next()
is the default value if no values meet your criteria.
What you describe has a general implementation called first_true
in the itertools recipes
.
def first_true(iterable, default=False, pred=None):
"""Returns the first true value in the iterable.
If no true value is found, returns *default*
If *pred* is not None, returns the first item
for which pred(item) is true.
"""
# first_true([a,b,c], x) --> a or b or c or x
# first_true([a,b], x, f) --> a if f(a) else b if f(b) else x
return next(filter(pred, iterable), default)
value = first_true([x, y, z], pred=lambda x: len(x) == 1)
if value:
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With