Created
June 30, 2019 06:22
-
-
Save melvinkcx/224585075f54f35cec40ca3dba87f5ab to your computer and use it in GitHub Desktop.
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