Multiple if statements in a lambda function

You are missing an else before 'O'. This works:

y = lambda symbol: 'X' if symbol==True else 'O' if symbol==False else ' '

However, I think you should stick to Adam Smith's approach. I find that easier to read.


This is one way where you can try multiple if else in lambda function

Example,

largest_num = lambda a,b,c : a if a>b and a>c else b if b>a and b>c else c if c>a and c>b else a

largest_num(3,8,14) will return 14


You can use an anonymous dict inside your anonymous function to test for this, using the default value of dict.get to symbolize your final "else"

y = lambda sym: {False: 'X', True: 'Y'}.get(sym, ' ')

Tags:

Python

Lambda