-1

Can we use pass keyword inside python lambda function or can I use only "If" condition without else in python lambda function?

I have tried:

f=lambda x: True if x % 2 == 0 else pass

and

f=lambda x: True if x % 2 == 0
aurelien
  • 404
  • 4
  • 22

2 Answers2

1

Since you have to return something anyway, why not just return the evaluated Boolean result.

>>> f=lambda x: x % 2 == 0
>>> f(2)
True
>>> f(3)
False
>>> 

If you are adamant about not returing anything for False case, you can do it in such a way:

>>> f=lambda x: x % 2 == 0 or None
>>> f(2)
True
>>> f(3)
>>> 
riteshtch
  • 8,629
  • 4
  • 25
  • 38
0

No, but you don't need to. You don't need if at all, because your condition by itself evaluates to a boolean. Just do:

f = lambda x: x % 2 == 0
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895