The syntax you're looking for:

lambda x: True if x % 2 == 0 else False

But you can't use print or raise in a lambda.

Answer from Robert Rossney on Stack Overflow
🌐
AskPython
askpython.com › python › examples › lambda-with-if-else-conditions
Lambda With Conditional Statements in Python - AskPython
December 31, 2021 - The statement says that if x is less than 20 divide it by 100 else print it as it is. Then we call the conditional_lambda function and inside it, we place the parameter as 4.
Discussions

Lambda if statement
You may be better just writing a normal function if it gets too complicated to understand, but for the sake of learning: something['SomeVariable'].apply(lambda x: dosomething() if isinstance(x, str) else dosomethingelse()) More on reddit.com
🌐 r/learnpython
9
6
August 18, 2015
if statement - if and else in python lambda expression - Stack Overflow
Please help me to understand how this works. Output is 4 a=4 b=7 x=lambda: a if 1 else b lambda x: 'big' if x > 100 else 'small' print(x()) More on stackoverflow.com
🌐 stackoverflow.com
can I use an if statement with a lambda function?
It helps to debug if you split your 1 liner into multiple variable steps at each function. I haven't worked with python but it appears the problem may be in your last map. It appears what you want is to filter for len(x['pdd_list']) == 0 in a filter function. .map(lambda x: x['pdd_list']) . filter(lambda x: len(x['pdd_list'])==0) More on reddit.com
🌐 r/apachespark
4
4
August 12, 2019
If statement in lambdas Python - Stack Overflow
I wanted to declare an if statement inside a lambda function: Suppose: cells = ['Cat', 'Dog', 'Snake', 'Lion', ...] result = filter(lambda element: if 'Cat' in element, cells) Is it possible to f... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-use-if-else-elif-in-python-lambda-functions
How to use if, else & elif in Python Lambda Functions - GeeksforGeeks
July 23, 2025 - The lambda function will return a value for every validated input. Here, if block will be returned when the condition is true, and else block will be returned when the condition is false.
🌐
thisPointer
thispointer.com › home › functions › python : how to use if, else & elif in lambda functions
Python : How to use if, else & elif in Lambda Functions - thisPointer
April 30, 2023 - *** Using if else in Lambda function *** True False False *** Creating conditional lambda function without if else *** True False False *** Using filter() function with a conditional lambda function (with if else) *** Original List : [1, 3, 33, 12, 34, 56, 11, 19, 21, 34, 15] Filtered List : [12, 11, 19, 15] *** Using if, elif & else in Lambda function *** convert 5 to : 10 convert 13 to : 39 convert 23 to : 23 · Why do we need Lambda functions in Python ?
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-use-if-else-amp-elif-in-python-lambda-functions
How to use if, else & elif in Python Lambda Functions
March 27, 2026 - numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Using with map() to categorize numbers categories = list(map(lambda x: "Small" if x <= 3 else "Medium" if x <= 7 else "Large", numbers)) print(categories) # Using with filter() to get specific values filtered_nums = list(filter(lambda x: "Even" if x % 2 == 0 else None, numbers)) print([x for x in numbers if (lambda y: True if y % 2 == 0 else False)(x)]) ['Small', 'Small', 'Small', 'Medium', 'Medium', 'Medium', 'Medium', 'Large', 'Large', 'Large'] [2, 4, 6, 8, 10] Lambda functions with conditional statements provide a powerful way to create concise, inline decision-making logic in Python.
Find elsewhere
🌐
EyeHunts
tutorial.eyehunts.com › home › python lambda if statement | example code
Python lambda if statement | Example code
June 9, 2022 - You can use if statement with lambda function in Python. Python lambda if returns a value based on conditional logic.
🌐
Delft Stack
delftstack.com › home › howto › python › python lambda if
if...else in Lambda Function Python | Delft Stack
February 25, 2025 - Python’s lambda functions are a powerful feature that allows developers to create small, anonymous functions at runtime. They are particularly useful for short operations that can be defined in a single line. However, integrating conditional logic, such as if statements, within these lambda functions can enhance their functionality significantly.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python lambda using if else
Python Lambda using if else - Spark By {Examples}
May 31, 2024 - How to use an if else in Python lambda? You can use the if-else statement in a lambda as part of an expression. The lambda should have only one expression
🌐
Python Examples
pythonexamples.org › python-lambda-if-else
Python Lambda - If Else, Nested If Else
In the following example program, we will write a lambda function that returns the number as is if divisible by 10, square of number if number is even, else cube of the number. x = lambda n: n if n == 0 else ( n**2 if n%2 == 0 else n**3 ) print(x(4)) print(x(3)) print(x(10)) ... Summarizing ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › lambda-with-if-but-without-else-in-python
Lambda with if but without else in Python - GeeksforGeeks
July 23, 2025 - Since a lambda function must have a return value for every valid input, we cannot define it with if but without else as we are not specifying what will we return if the if-condition will be false i.e.
Top answer
1 of 8
121

Use the exp1 if cond else exp2 syntax.

rate = lambda T: 200*exp(-T) if T>200 else 400*exp(-T)

Note you don't use return in lambda expressions.

2 of 8
28

The right way to do this is simple:

def rate(T):
    if (T > 200):
        return 200*exp(-T)
    else:
        return 400*exp(-T)

There is absolutely no advantage to using lambda here. The only thing lambda is good for is allowing you to create anonymous functions and use them in an expression (as opposed to a statement). If you immediately assign the lambda to a variable, it's no longer anonymous, and it's used in a statement, so you're just making your code less readable for no reason.

The rate function defined this way can be stored in an array, passed around, called, etc. in exactly the same way a lambda function could. It'll be exactly the same (except a bit easier to debug, introspect, etc.).


From a comment:

Well the function needed to fit in one line, which i didn't think you could do with a named function?

I can't imagine any good reason why the function would ever need to fit in one line. But sure, you can do that with a named function. Try this in your interpreter:

>>> def foo(x): return x + 1

Also these functions are stored as strings which are then evaluated using "eval" which i wasn't sure how to do with regular functions.

Again, while it's hard to be 100% sure without any clue as to why why you're doing this, I'm at least 99% sure that you have no reason or a bad reason for this. Almost any time you think you want to pass Python functions around as strings and call eval so you can use them, you actually just want to pass Python functions around as functions and use them as functions.

But on the off chance that this really is what you need here: Just use exec instead of eval.

You didn't mention which version of Python you're using. In 3.x, the exec function has the exact same signature as the eval function:

exec(my_function_string, my_globals, my_locals)

In 2.7, exec is a statement, not a function—but you can still write it in the same syntax as in 3.x (as long as you don't try to assign the return value to anything) and it works.

In earlier 2.x (before 2.6, I think?) you have to do it like this instead:

exec my_function_string in my_globals, my_locals
🌐
Kanoki
kanoki.org › 2022 › 09 › 12 › python-lambda-if-else-elif-with-multiple-conditions-and-filter-list-with-conditions-in-lambda
python lambda if, else & elif with multiple conditions and filter list with conditions in lambda | kanoki
September 12, 2022 - We can write this multiple conditions in lambda as shown below · grade = lambda x: 'A+' if x>7 else 'A' if x>=5 and x<=7 else 'B' ... grade = lambda score : ((((('Not sure' , 'B')[score < 5]), 'A')[(score>=5 | score<=7)]), 'A+')[score>7] ... We could also evaluate a statement if it’s True ...
🌐
TutorialsPoint
tutorialspoint.com › lambda-with-if-but-without-else-in-python
Lambda with if but without else in Python
July 17, 2023 - While lambda functions with if statements typically require an else clause due to Python's syntax, you can achieve "if without else" behavior using built-in functions like filter() and map().