What is the alternative for *if elif else* ladder in Python?

10/9/2019 4:28:43 AM


3 Answers

New Answer


There are no strict alternatives for if elif else statements, such as switch statement which I assume you to have been searching for.


You can get rid of elif by nesting more if else statements within the else but elif helps reduce the levels of nesting.


Depending how complicated your conditions are. You can use a dictionary to drive a condition, where the key is the result of the conditional expression, and the value could even be a function call. Also you can use map() to transform values.