This is the same as Eli Barzilay's answer, but instead of wrapping it in lambda, I will wrap it in a list of 1 element
(if test conseq altern) => (car (or (and test (list conseq)) (list altern)))
By the way, Python before 2.5 had this exact problem. There was no good way to write a conditional expression (i.e. test ? conseq : altern in C, which is if in the schema) in Python. The simplest attempt was
test and conseq or altern
which worked in most cases but failed when conseq is considered false in Python (i.e. False, 0, empty list, empty string, etc.). What kind of problem did you find above. The fix was to wrap it in a list (non-empty lists are always true) and extract it again:
(test and [conseq] or [altern])[0]
which looks ugly. That's why they added the conseq if test else altern in Python 2.5.
source share