Skip to content

Instantly share code, notes, and snippets.

@melvinkcx
Created June 30, 2019 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melvinkcx/224585075f54f35cec40ca3dba87f5ab to your computer and use it in GitHub Desktop.
Save melvinkcx/224585075f54f35cec40ca3dba87f5ab to your computer and use it in GitHub Desktop.
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