Filtering Dictionary in Python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Dict | |
def get_menu() -> Dict[str, dict]: | |
return { | |
"TIMMY_BLACK": { | |
"item": "Timmy's Coffee Barista's Black", | |
"sugar_free": True, | |
"with_milk": False, | |
}, | |
"TIMMY_LATTE": { | |
"item": "Timmy's Coffee Latte", | |
"sugar_free": True, | |
"with_milk": False, | |
}, | |
"TIMMY_SWEETENED_LATTE": { | |
"item": "Timmy's Coffee Latte - Sweetened", | |
"sugar_free": True, | |
"with_milk": True, | |
}, | |
"TIMMY_SIGNATURE_CARAMEL": { | |
"item": "Timmy's Signature - Caramel Latte", | |
"sugar_free": None, # Unknown | |
"with_milk": True, | |
} | |
} | |
coffees = get_menu() | |
# Filter unsweetened coffees -> RuntimeError | |
for code, details in coffees.items(): | |
if details.get("sugar_free") is True: | |
del coffees[code] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment