Created
August 6, 2020 06:09
-
-
Save raeq/3c0a5193d526301e46d9c032505fbf8c to your computer and use it in GitHub Desktop.
Remove items from a dictionary with matching value
This file contains hidden or 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
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