Skip to content

Instantly share code, notes, and snippets.

@raeq
Created August 6, 2020 06:09
Show Gist options
  • Save raeq/3c0a5193d526301e46d9c032505fbf8c to your computer and use it in GitHub Desktop.
Save raeq/3c0a5193d526301e46d9c032505fbf8c to your computer and use it in GitHub Desktop.
Remove items from a dictionary with matching value
def remove_even_items(the_dict: dict):
# collect the keys of all the items to remove
delete_these = set(k for k, v in the_dict.items() if v % 2 == 0)
for delete_this in delete_these:
del the_dict[delete_this]
return the_dict
dict1: dict = dict(enumerate(range(13)))
assert remove_even_items(dict1) == {1: 1, 3: 3, 5: 5, 7: 7, 9: 9, 11: 11}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment