498

In Python 2.6, I want to do:

f = lambda x: if x==2 print x else raise Exception()
f(2) #should print "2"
f(3) #should throw an exception

This clearly isn't the syntax. Is it possible to perform an if in lambda and if so how to do it?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Guy
  • 14,178
  • 27
  • 67
  • 88
  • 3
    You can't print or raise in a lambda. Lambdas are just functions, you can alwaya use a function instead. – Lennart Regebro Oct 18 '09 at 17:36
  • 11
    I disagree with you. I need 4 different, very short functions like the one above that need to be put in a list/dictionary so I can iterate over them and select which ones to use in each iteration. Instead of many lines of code of just inits, before the iteration, itself I can bring it down to only 4 lines of init code. The less the merrier.. – Guy Oct 18 '09 at 21:20
  • 6
    4 lines of code is not a laudable solution when other people have to read, interpret, understand and maintain the code. Further, the "print/raise" problem in the example shows this which cannot and should not be done in lambdas. – S.Lott Oct 18 '09 at 22:12
  • @LennartRegebro lambdas are not functions in python, they are only expressions, that is why there are many things you can not do with them. – Aaron McMillin Aug 11 '16 at 15:35
  • 3
    @AaronMcMillin Lambdas are functions. They are restricted to expressions for syntax reasons, but they ARE functions. – Lennart Regebro Aug 17 '16 at 10:06
  • I think the important point to make here is that lambda's aren't maintained in memory like functions, they are generic functions that are evaluated and discarded. If you want to loop over functions you need a more permanent structure like an event. – Jamie Marshall Jul 19 '18 at 18:21

16 Answers16

876

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.

Robert Rossney
  • 94,622
  • 24
  • 146
  • 218
  • 48
    in python 3, you can use print – recursive Oct 18 '09 at 17:09
  • 111
    It's a horrible syntax--easily the worst Python language construct, approaching Perl levels of absurdity in its out-of-order evaluation--but it's what was asked for. You're seriously voting down answers for being correct? – Glenn Maynard Oct 18 '09 at 21:52
  • 10
    @Glenn Maynard: it's not the syntax that horrifying. It's any expression of the form `True if expression else False`. The `if` construct is totally redundant and therefore deeply and horrifyingly confusing. It's as bad as the statement form: `if expression: return True`. – S.Lott Oct 18 '09 at 22:14
  • 9
    Well, it's certainly confusing if an otherwise-smart person can't realize that it's the correct answer to the question that was asked, which was entirely about syntax. I regret that the example I has apparently given you the horrors. Feel free to construct a more appropriate one. – Robert Rossney Oct 19 '09 at 07:38
  • 6
    @Robert Rossney: the trivial redundancy of `True if expression else False` is horrifying. The correct form is shown by @unutbu: `lambda x: x%2==0`. There's no horrifying `True if True else False` in that formulation. – S.Lott Oct 19 '09 at 10:37
  • 46
    It's the correct answer to the question "How do I write a lambda function that tells me if a number is even?" It is not, however, a correct answer to the question that the OP originally asked. However much you don't like the example I contrived, my post *does*, in fact, clearly answer the OP's question. – Robert Rossney Oct 19 '09 at 17:34
  • 14
    It's painfully obvious that anyone suggesting "x%2==0"--or voting up a comment recommending it, which makes at least seven people--didn't even read the original question. – Glenn Maynard Oct 19 '09 at 22:48
  • 1
    @S.Lott, but if we want return something else than boolean the syntax above is required. Moreover it's the way how comprehensions currently work in Python. – Marek Marczak Nov 07 '18 at 06:38
  • 1
    @GlennMaynard I think you need to read the question. I like this answer – Ice Bear Jan 06 '21 at 14:00
  • 1
    @recursive, how print is used in case like `df.apply(lambda x: print(x)if x.isna().sum()>0)`? Keep getting the `invalid syntax` error and can not find a solution. – Jia Gao Aug 17 '21 at 05:23
  • You're missing `else`. Syntax is `a if b else c`. – recursive Aug 17 '21 at 17:13
45

why don't you just define a function?

def f(x):
    if x == 2:
        print(x)
    else:
        raise ValueError

there really is no justification to use lambda in this case.

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
  • 4
    `print` is not a function in 2.6 yet. :) – Lukáš Lalinský Oct 18 '09 at 16:52
  • 7
    @Lukáš Lalinský: it still works in 2.x. it will be treated as a pair of redundant parentheses – newacct Oct 18 '09 at 18:28
  • 36
    You don't know his actual use case, so there's no way you can say there's no reason to use a lambda. – Glenn Maynard Oct 18 '09 at 21:52
  • 7
    @Glenn Maynard: There's almost no reason to use a lambda, period. Assigning a lambda to a variable -- as a stand-in for `def` -- is generally a Very Bad Idea (tm). Just use a `def` so mere mortal programmers can read, interpret, understand and maintain it. – S.Lott Oct 18 '09 at 22:15
  • 21
    There are plenty of legitimate uses of lambdas. If you can't think of any, then that's not lambda's fault. (I'm not a fan of the syntax itself, of course--it's a clumsy workaround for the fact that Python's poorly-conceived indentation syntax can't handle inline functions like normal languages.) – Glenn Maynard Oct 19 '09 at 22:57
  • 2
    lambdas are most useful if you need to, for whatever reason, dynamically construct short functions or object factories at runtime. In the latter case, is probably better than `deecopy`. – cowbert Dec 08 '17 at 21:11
  • I agree with Mr Maynard, a lot of people use lamda just for simplifying code, some people think they look cleaner – Keith Mak Sep 06 '22 at 05:34
42

Probably the worst python line I've written so far:

f = lambda x: sys.stdout.write(["2\n",][2*(x==2)-2])

If x == 2 you print,

if x != 2 you raise.

jimifiki
  • 5,377
  • 2
  • 34
  • 60
28

You can easily raise an exception in a lambda, if that's what you really want to do.

def Raise(exception):
    raise exception
x = lambda y: 1 if y < 2 else Raise(ValueError("invalid value"))

Is this a good idea? My instinct in general is to leave the error reporting out of lambdas; let it have a value of None and raise the error in the caller. I don't think this is inherently evil, though--I consider the "y if x else z" syntax itself worse--just make sure you're not trying to stuff too much into a lambda body.

Glenn Maynard
  • 55,829
  • 10
  • 121
  • 131
  • Possibly, but it depends heavily on the particular case. Of course, you can also decorate the lambda after creating it. `x = RaiseValueErrorOnNone(x)`, again, depending on the case. – Glenn Maynard Oct 19 '09 at 22:42
21

note you can use several else...if statements in your lambda definition:

f = lambda x: 1 if x>0 else 0 if x ==0 else -1
filotn
  • 546
  • 4
  • 8
16

Lambdas in Python are fairly restrictive with regard to what you're allowed to use. Specifically, you can't have any keywords (except for operators like and, not, or, etc) in their body.

So, there's no way you could use a lambda for your example (because you can't use raise), but if you're willing to concede on that… You could use:

f = lambda x: x == 2 and x or None
David Wolever
  • 148,955
  • 89
  • 346
  • 502
4

This snippet should help you:

x = lambda age: 'Older' if age > 30 else 'Younger'

print(x(40))
Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
3

If you still want to print you can import future module

from __future__ import print_function

f = lambda x: print(x) if x%2 == 0 else False
2

You can also use Logical Operators to have something like a Conditional

func = lambda element: (expression and DoSomething) or DoSomethingIfExpressionIsFalse

You can see more about Logical Operators here

  • It doesn't go with python's philosophy in terms of clarity. Even though logically equivalent, the `if` syntax is always preferred over this. _The obvious_ way of checking conditions. – 0xc0de Jul 12 '18 at 12:28
  • Thank you! I used this in a work of functional language in college because of restrictions that the professor has imposed who says that I couldn't use `if` statement, so I found this _not obvious_ way. – Victor Lucas Jul 14 '18 at 12:03
2

the solution for the given scenerio is:

f = lambda x : x if x == 2 else print("number is not 2")
f(30)  # number is not 2
f(2)   #2
Luca Faggianelli
  • 2,032
  • 20
  • 23
abhi
  • 65
  • 3
  • This is valid in Python 3.x, but the question was about Python 2.6. In Python 2.x this answer is not correct. – wovano Nov 21 '21 at 11:41
1

what you need exactly is

def fun():
    raise Exception()
f = lambda x:print x if x==2 else fun()

now call the function the way you need

f(2)
f(3)
  • This gives `SyntaxError: invalid syntax`. I've tested this in Python 2.7 since I don't have Python 2.6 installed, but I really can't imagine this is valid in Python 2.6, since [you can't use print in a lambda expression](https://stackoverflow.com/q/2970858/10669875). – wovano Nov 21 '21 at 11:40
1

An easy way to perform an if in lambda is by using list comprehension.

You can't raise an exception in lambda, but this is a way in Python 3.x to do something close to your example:

f = lambda x: print(x) if x==2 else print("exception")

Another example:

return 1 if M otherwise 0

f = lambda x: 1 if x=="M" else 0
Paul Cysne
  • 61
  • 5
1

Here's the solution if you use Python 3.x!

>>> f = lambda x: print(x) if x == 2 else print("ERROR")
>>> f(23)
ERROR
>>> f(2)
2
>>> 
Rustam-Z
  • 113
  • 1
  • 9
  • This is valid in Python 3.x, but the question was about Python 2.6. In Python 2.x this answer does not work, since [you can't use print in a lambda expression](https://stackoverflow.com/q/2970858/10669875). – wovano Nov 21 '21 at 11:46
0

Following sample code works for me. Not sure if it directly relates to this question, but hope it helps in some other cases.

a = ''.join(map(lambda x: str(x*2) if x%2==0 else "", range(10)))
Rahul
  • 205
  • 2
  • 8
0

Hope this will help a little

you can resolve this problem in the following way

f = lambda x:  x==2   

if f(3):
  print("do logic")
else:
  print("another logic")
Anurag Bhakuni
  • 2,379
  • 26
  • 32
0

it might be worth considering np.where

ZakS
  • 1,073
  • 3
  • 15
  • 27