Last active
January 25, 2023 04:04
-
-
Save lucascoelhof/158980602ddfc90b0da52b10d3ce06b7 to your computer and use it in GitHub Desktop.
How to delete a dictionary key that matches a regex (regular expression) in python
This file contains 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
pattern = re.compile(regex) | |
matched_keys = {key for key, value in dic.iteritems() if pattern.match(key)} | |
for key in matched_keys: | |
dic.pop(key, None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment