In R there is function ifelse(test_expression, x, y)
, does Python have smth like this?
Asked
Active
Viewed 55 times
-1

Artem T
- 27
- 3
-
Why ask a question and then immediately answer it? – neuron Nov 21 '21 at 22:03
-
1@neuron if you figure out something that might be useful to other people, it is a useful way to record and share that information. In this case the question happens to be a duplicate, so it should get flagged as such and closed. – shadowtalker Nov 21 '21 at 22:38
2 Answers
1
With help from this answer about a ternary form of if
:
def ifelse(t, x, y):
return list(map(lambda t, x, y: x if t else y, t, x, y ))
I don't do much Python so this may be sub-optimal/non-pythonic ...

Ben Bolker
- 211,554
- 25
- 370
- 453