Skip to content

Instantly share code, notes, and snippets.

@ambv
Created May 21, 2016 23:52
Show Gist options
  • Save ambv/c6efa22e21de9b6b22147595cdc93d44 to your computer and use it in GitHub Desktop.
Save ambv/c6efa22e21de9b6b22147595cdc93d44 to your computer and use it in GitHub Desktop.
Requires Python 3.3+
from contextlib import ExitStack
d = {index: str(index) for index in range(100)}
with ExitStack() as stack:
for key in d:
if key % 13 == 0:
stack.callback(d.pop, key)
print(d)
@msztolcman
Copy link

IMHO simpler and good enough in most cases is:

for key in list(d.keys()):
  if key % 13 == 0:
    del d[key]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment