Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Filtering Dictionary in Python 3
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
You can’t perform that action at this time.