Steph
Posted on March 26, 2024
If/elif/else statements are great for handling conditional statements in Python, but what if you have a very long list of conditions? Writing out all of the if/elif/else statements can get very tedious and repetitive. Python has two other options for handling conditional statements that can be valuable in this situation: dictionary mapping and match/case statements. We’ll dive into both of them in more detail below.
Dictionary Mapping
Let’s say you have the following if/elif/else statements representing a toddler’s activities based on their emotions:
toddler = "happy"
if toddler == "hungry":
activity = "Snack time, asap!"
elif toddler == "sleepy":
activity = "Read 800 Little Blue Truck books."
elif toddler == "happy":
activity = "Play with anything other than safe, age-appropriate toys."
elif toddler == "snuggly":
activity = "Give mom snugs."
elif toddler == "curious":
activity = "Try to break into all locked cupboards."
else:
activity = "Run circles around tired parents."
print(activity)
In the code above, “if/elif toddler ==” and “activity ==” are repeated over and over, so it’s not particularly DRY. Here’s how the same code would look using dictionary mapping:
toddler = "happy"
toddler_activities = {
"hungry": "Snack time, asap!",
"sleepy": "Read 800 Little Blue Truck books.",
"happy": "Play with anything other than safe, age-appropriate toys.",
"snuggly": "Give mom snugs.",
"curious": "Try to break into all locked cupboards."
}
activity = toddler_activities.get(toddler, "Run circles around tired parents.")
print(activity)
In this version, the dictionary’s keys represent the toddler's emotions and the values represent the corresponding activities. We then use the get() method to retrieve the activity based on the toddler's current emotion. If the emotion is not found in the dictionary, the default activity is "Run circles around tired parents."
Match/Case Statements
Another approach to handling long lists of conditionals is to use match/case statements. This approach was introduced in Python 3.10 and is very similar to switch/case statements in JavaScript. Here’s how our code would look using match/case statements:
toddler = "happy"
activity = match toddler:
case "hungry":
"Snack time, asap!"
case "sleepy":
"Read 800 Little Blue Truck books."
case "happy":
"Play with anything other than safe, age-appropriate toys."
case "snuggly":
"Give mom snugs."
case "curious":
"Try to break into all locked cupboards."
case _:
"Run circles around tired parents."
print(activity)
The “match” statement is used to match the value of “toddler” against the various cases, each corresponding to a toddler emotion. If no match is found ( “case_:”), the default activity is "Run circles around tired parents."
When to Use Each Approach
Ultimately the choice between if/elif/else statements, dictionary mapping, and match/case statements depends on the specific requirements and preferences of your project:
- While if/elif/else statements are great to use when you have a small list of conditionals, they can quickly become repetitive when the list becomes long.
- Using dictionary mapping is a DRYer and more concise approach, however, it’s not necessarily as easy to read as if/elif/else statements because you have to read through key/value pairs.
- If you’re using Python 3.10 or higher, match/case statements are a great option to use since they are both DRYER than if/elif/else statements and also more intuitive to read than dictionary mapping.
Sources:
- Flatiron Python Curriculum, “Control Flow: Conditional Statements” Lesson
- Geeks for Geeks, “Python Match-Case Statement”
Posted on March 26, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 1, 2024
October 6, 2024