Skip to content

Instantly share code, notes, and snippets.

@lucascoelhof
Last active January 25, 2023 04:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucascoelhof/158980602ddfc90b0da52b10d3ce06b7 to your computer and use it in GitHub Desktop.
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
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