I was writing some lambda functions and couldn’t figure this out. Is there a way to have something like lambda x: x if (x<3) in python? As lambda a,b: a if (a > b) else b works ok. So far lambda x: x < 3 and x or None seems to be the closest i have found.
I was writing some lambda functions and couldn’t figure this out. Is there a
Share
A lambda, like any function, must have a return value.
lambda x: x if (x<3)does not work because it does not specify what to return if notx<3. By default functions returnNone, so you could doBut perhaps what you are looking for is a list comprehension with an
ifcondition. For example: