Compressing `x if x else y` statement in Python Compressing `x if x else y` statement in Python python python

Compressing `x if x else y` statement in Python


There is no way to do this, and that's intentional. The ternary if is only supposed to be used for trivial cases.

If you want to use the result of a computation twice, put it in a temporary variable:

value = info.findNext("b")value = value if value else "Oompa Loompa"

Once you do this, it becomes clear that you're doing something silly, and in fact the pythonic way to write this is:

value = info.findNext("b")if not value:    value = "Oompa Loompa"

And that's actually 5 fewer keystrokes than your original attempt.

If you really want to save keystrokes, you can instead do this:

value = info.findNext("b") or "Oompa Loompa"

But that's discouraged by many style guides, and by the BDFL.

If you're only doing this once, it's better to be more explicit. If you're doing it half a dozen times, it's trivial—and much better—to make findNext take an optional default to return instead of None, just like all those built-in and stdlib functions:

def findNext(self, needle, defvalue=None):    # same code as before, but instead of return None or falling off the end,    # just return defvalue.

Then you can do this:

value = info.findNext("b", "Oompa Loompa")


Don't use if ... else at all. Instead, take advantage of Python's coalescing operators.

value = info.findNext("b") or "Oompa Loompa"