Using Dictionaries In Place of If-conditions
Dictionaries are mainly used as a data structure in Python for maintaining key-value pairs.
However, there's another special use case that dictionaries can handle. This is — Eliminating IF conditions from your code.
Consider the code snippet above. Here, corresponding to an input value, we invoke a specific function. The traditional way requires you to hard-code every case.
But with a dictionary, you can directly retrieve the corresponding function by providing it with the key. This makes your code concise and elegant.
Share this post on LinkedIn: Post Link.
Thanks for reading Daily Dose of Data Science! Subscribe for free to learn something new and insightful about Python and Data Science every day. Also, get a Free Data Science PDF (250+ pages) with 200+ tips.