What is the alternative for *if elif else* ladder in Python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

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

9th Oct 2019, 4:28 AM
APURVA JAISWAL
APURVA JAISWAL - avatar
3 Answers
+ 5
There are no strict alternatives for if elif else statements, such as switch statement which I assume you to have been searching for.
9th Oct 2019, 4:43 AM
Seb TheS
Seb TheS - avatar
+ 5
You can get rid of elif by nesting more if else statements within the else but elif helps reduce the levels of nesting.
9th Oct 2019, 4:55 AM
Sonic
Sonic - avatar
+ 3
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.
9th Oct 2019, 4:57 AM
Tibor Santa
Tibor Santa - avatar