- Daily Dose of Data Science
- Posts
- Using Python Dictionaries as a Potential Alternative to IF Conditions
Using Python Dictionaries as a Potential Alternative to IF Conditions
A lesser-known usage of dictionaries in Python.
Dictionaries are mainly used as a data structure in Python for maintaining key-value pairs.
But unknown to many, thereβs another special use case of dictionaries β using them as a potential alternative to IF conditions.
For instance, consider the code snippet below. 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.
Isnβt that cool, elegant, and neat?
π Over to you: What other lesser-known Python hacks are you aware of?
π If you liked this post, donβt forget to leave a like β€οΈ. It helps more people discover this newsletter on Substack and tells me that you appreciate reading these daily insights. The button is located towards the bottom of this email.
Thanks for reading!
Whenever youβre ready, here are a couple of more ways I can help you:
Get the full experience of the Daily Dose of Data Science. Every week, receive two deep dives that:
Make you fundamentally strong at data science and statistics.
Help you approach data science problems with intuition.
Teach you concepts that are highly overlooked or misinterpreted.
Promote to 33,000 subscribers by sponsoring this newsletter.
π Tell the world what makes this newsletter special for you by leaving a review here :)
π If you love reading this newsletter, feel free to share it with friends!
Reply