Skip to content

Instantly share code, notes, and snippets.

@melvinkcx
Created June 30, 2019 06:37
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/6ec13e00dbcd453d120e173a2338e457 to your computer and use it in GitHub Desktop.
Save melvinkcx/6ec13e00dbcd453d120e173a2338e457 to your computer and use it in GitHub Desktop.
Filtering Dictionary in Python
"""
Solution #3: Deferred Update
Risk: What if an Exception is raised half way?
"""
coffees = get_menu()
keys_to_be_removed = []
for code, details in coffees.items():
if details.get("sugar_free") is True:
keys_to_be_removed.append(code)
for k in keys_to_be_removed:
del coffees[k]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment