Python Ternary Lambda

Problem: How to write the ternary operator in a lambda function?

Example: Say, you’ve got the following example:

def f(x):
    if x > 100:
        x = 1.1*x
        x = 1.05*x
    return x

# 105.0

The function f(x) takes one argument x and increases it by 10% if the argument is larger than 100. Otherwise, it increases it by 5%.

In this article, you’ll learn how to convert this code snippet into a Python One-Liner by using the Ternary operator—so stay tuned!

But first things first: we start with a short explanation of the ternary operator and the lambda function. If you already know these Python concepts very well, you can skip them and go right away to the solution.

Short Recap: Ternary Operator

Ternary Operator: The most basic ternary operator x if c else y consists of three operands x, c, and y. It is an expression with a return value. The ternary operator returns x if the Boolean expression c evaluates to True. Otherwise, if the expression c evaluates to False, the ternary operator returns the alternative y.

Syntax: The three operands are written as x if c else y which reads as “return x if c else return y“. Let’s write this more intuitively as:

<OnTrue> if <Condition> else <OnFalse>
<OnTrue>The return expression of the operator in case the condition evaluates to True
<Condition>The condition that determines whether to return the <On True> or the <On False> branch.
<OnFalse>The return expression of the operator in case the condition evaluates to False
Operands of the Ternary Operator

Related article: For a full tutorial on the ternary operator, check out our detailed blog article.

Short Recap: Lambda Function

A lambda function is an anonymous function in Python. It starts with the keyword lambda, followed by a comma-separated list of zero or more arguments, followed by the colon and the return expression. For example, lambda x, y, z: x+y+z would calculate the sum of the three argument values x+y+z.

Here’s a practical example where lambda functions are used to generate an incrementor function:

Exercise: Add another parameter to the lambda function!

Watch the video or read the article to learn about lambda functions in Python:

Now, you know everything you need to know to shorten the above code snippet!

Method: Using the Ternary Operator in a Lambda Function

As it turns out, you can also use the ternary operator effectively:

f = lambda x: 1.1*x if x>100 else 1.05*x

# 105.0

The result is the same. An intermediate to advanced Python coder will have no problem understanding the code and it’s much more concise. That’s why I’d prefer this way over the first one.

Here’s a direct one-on-one comparison of both methods. Which one do you like most?

Try it yourself:

Exercise: Before you run the code, take a guess: what’s the output of this code puzzle?

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!