-7

I tried to find best way of having ternary operator in python, but didn't find conclusive information. Is using lambda, as shown below, the best way? What is the best way to do below operation in one line?

return (lambda: "Yes", lambda: "No") ['hello' == 'hello']()
user3373861
  • 13
  • 1
  • 8

1 Answers1

0

Python does have a ternary operator if you use an If-else:

return 'Yes' if 'hello' == 'hello' else 'No'

You could also use a dictionary or list, but I don’t see why you would:

return ['No', 'Yes']['hello' == 'hello']

return {1: 'Yes'}.get('hello' == 'hello', 'No')
N Chauhan
  • 3,407
  • 2
  • 7
  • 21
  • If I found either of the last two obfuscations in any project I work on, I'd have strong words for whoever put it there... – kungphu Oct 15 '18 at 06:10