Daily Dose of Data Science

Share this post

Using Dictionaries In Place of If-conditions

www.blog.dailydoseofds.com

Using Dictionaries In Place of If-conditions

Avi Chawla
Nov 21, 2022
5
1
Share
Share this post

Using Dictionaries In Place of If-conditions

www.blog.dailydoseofds.com

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.


I like to explore, experiment and write about data science concepts and tools. You can read my articles on Medium. Also, you can connect with me on LinkedIn.

5
1
Share
Share this post

Using Dictionaries In Place of If-conditions

www.blog.dailydoseofds.com
Previous
Next
1 Comment
Shreyash Gupta
Nov 21, 2022

Nice bhaiya😍

Expand full comment
Reply
Top
New
Community

No posts

Ready for more?

© 2023 Avi Chawla
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing